xref: /openbmc/linux/net/core/rtnetlink.c (revision 39fe5434cb9de5da40510028b17b96bc4eb312b3)
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 
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>
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>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
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>
52 #include <net/fib_rules.h>
53 #include <net/rtnetlink.h>
54 
55 struct rtnl_link
56 {
57 	rtnl_doit_func		doit;
58 	rtnl_dumpit_func	dumpit;
59 };
60 
61 static DEFINE_MUTEX(rtnl_mutex);
62 static struct sock *rtnl;
63 
64 void rtnl_lock(void)
65 {
66 	mutex_lock(&rtnl_mutex);
67 }
68 
69 void __rtnl_unlock(void)
70 {
71 	mutex_unlock(&rtnl_mutex);
72 }
73 
74 void rtnl_unlock(void)
75 {
76 	mutex_unlock(&rtnl_mutex);
77 	if (rtnl && rtnl->sk_receive_queue.qlen)
78 		rtnl->sk_data_ready(rtnl, 0);
79 	netdev_run_todo();
80 }
81 
82 int rtnl_trylock(void)
83 {
84 	return mutex_trylock(&rtnl_mutex);
85 }
86 
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 
100 int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
101 				 struct rtattr *rta, int len)
102 {
103 	if (RTA_PAYLOAD(rta) < len)
104 		return -1;
105 	if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
106 		rta = RTA_DATA(rta) + RTA_ALIGN(len);
107 		return rtattr_parse_nested(tb, maxattr, rta);
108 	}
109 	memset(tb, 0, sizeof(struct rtattr *) * maxattr);
110 	return 0;
111 }
112 
113 static struct rtnl_link *rtnl_msg_handlers[NPROTO];
114 
115 static inline int rtm_msgindex(int msgtype)
116 {
117 	int msgindex = msgtype - RTM_BASE;
118 
119 	/*
120 	 * msgindex < 0 implies someone tried to register a netlink
121 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
122 	 * the message type has not been added to linux/rtnetlink.h
123 	 */
124 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
125 
126 	return msgindex;
127 }
128 
129 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
130 {
131 	struct rtnl_link *tab;
132 
133 	tab = rtnl_msg_handlers[protocol];
134 	if (tab == NULL || tab[msgindex].doit == NULL)
135 		tab = rtnl_msg_handlers[PF_UNSPEC];
136 
137 	return tab ? tab[msgindex].doit : NULL;
138 }
139 
140 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
141 {
142 	struct rtnl_link *tab;
143 
144 	tab = rtnl_msg_handlers[protocol];
145 	if (tab == NULL || tab[msgindex].dumpit == NULL)
146 		tab = rtnl_msg_handlers[PF_UNSPEC];
147 
148 	return tab ? tab[msgindex].dumpit : NULL;
149 }
150 
151 /**
152  * __rtnl_register - Register a rtnetlink message type
153  * @protocol: Protocol family or PF_UNSPEC
154  * @msgtype: rtnetlink message type
155  * @doit: Function pointer called for each request message
156  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
157  *
158  * Registers the specified function pointers (at least one of them has
159  * to be non-NULL) to be called whenever a request message for the
160  * specified protocol family and message type is received.
161  *
162  * The special protocol family PF_UNSPEC may be used to define fallback
163  * function pointers for the case when no entry for the specific protocol
164  * family exists.
165  *
166  * Returns 0 on success or a negative error code.
167  */
168 int __rtnl_register(int protocol, int msgtype,
169 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
170 {
171 	struct rtnl_link *tab;
172 	int msgindex;
173 
174 	BUG_ON(protocol < 0 || protocol >= NPROTO);
175 	msgindex = rtm_msgindex(msgtype);
176 
177 	tab = rtnl_msg_handlers[protocol];
178 	if (tab == NULL) {
179 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
180 		if (tab == NULL)
181 			return -ENOBUFS;
182 
183 		rtnl_msg_handlers[protocol] = tab;
184 	}
185 
186 	if (doit)
187 		tab[msgindex].doit = doit;
188 
189 	if (dumpit)
190 		tab[msgindex].dumpit = dumpit;
191 
192 	return 0;
193 }
194 
195 EXPORT_SYMBOL_GPL(__rtnl_register);
196 
197 /**
198  * rtnl_register - Register a rtnetlink message type
199  *
200  * Identical to __rtnl_register() but panics on failure. This is useful
201  * as failure of this function is very unlikely, it can only happen due
202  * to lack of memory when allocating the chain to store all message
203  * handlers for a protocol. Meant for use in init functions where lack
204  * of memory implies no sense in continueing.
205  */
206 void rtnl_register(int protocol, int msgtype,
207 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
208 {
209 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
210 		panic("Unable to register rtnetlink message handler, "
211 		      "protocol = %d, message type = %d\n",
212 		      protocol, msgtype);
213 }
214 
215 EXPORT_SYMBOL_GPL(rtnl_register);
216 
217 /**
218  * rtnl_unregister - Unregister a rtnetlink message type
219  * @protocol: Protocol family or PF_UNSPEC
220  * @msgtype: rtnetlink message type
221  *
222  * Returns 0 on success or a negative error code.
223  */
224 int rtnl_unregister(int protocol, int msgtype)
225 {
226 	int msgindex;
227 
228 	BUG_ON(protocol < 0 || protocol >= NPROTO);
229 	msgindex = rtm_msgindex(msgtype);
230 
231 	if (rtnl_msg_handlers[protocol] == NULL)
232 		return -ENOENT;
233 
234 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
235 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
236 
237 	return 0;
238 }
239 
240 EXPORT_SYMBOL_GPL(rtnl_unregister);
241 
242 /**
243  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
244  * @protocol : Protocol family or PF_UNSPEC
245  *
246  * Identical to calling rtnl_unregster() for all registered message types
247  * of a certain protocol family.
248  */
249 void rtnl_unregister_all(int protocol)
250 {
251 	BUG_ON(protocol < 0 || protocol >= NPROTO);
252 
253 	kfree(rtnl_msg_handlers[protocol]);
254 	rtnl_msg_handlers[protocol] = NULL;
255 }
256 
257 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
258 
259 static LIST_HEAD(link_ops);
260 
261 /**
262  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
263  * @ops: struct rtnl_link_ops * to register
264  *
265  * The caller must hold the rtnl_mutex. This function should be used
266  * by drivers that create devices during module initialization. It
267  * must be called before registering the devices.
268  *
269  * Returns 0 on success or a negative error code.
270  */
271 int __rtnl_link_register(struct rtnl_link_ops *ops)
272 {
273 	if (!ops->dellink)
274 		ops->dellink = unregister_netdevice;
275 
276 	list_add_tail(&ops->list, &link_ops);
277 	return 0;
278 }
279 
280 EXPORT_SYMBOL_GPL(__rtnl_link_register);
281 
282 /**
283  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
284  * @ops: struct rtnl_link_ops * to register
285  *
286  * Returns 0 on success or a negative error code.
287  */
288 int rtnl_link_register(struct rtnl_link_ops *ops)
289 {
290 	int err;
291 
292 	rtnl_lock();
293 	err = __rtnl_link_register(ops);
294 	rtnl_unlock();
295 	return err;
296 }
297 
298 EXPORT_SYMBOL_GPL(rtnl_link_register);
299 
300 /**
301  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
302  * @ops: struct rtnl_link_ops * to unregister
303  *
304  * The caller must hold the rtnl_mutex.
305  */
306 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
307 {
308 	struct net_device *dev, *n;
309 
310 	for_each_netdev_safe(dev, n) {
311 		if (dev->rtnl_link_ops == ops)
312 			ops->dellink(dev);
313 	}
314 	list_del(&ops->list);
315 }
316 
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 }
329 
330 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
331 
332 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
333 {
334 	const struct rtnl_link_ops *ops;
335 
336 	list_for_each_entry(ops, &link_ops, list) {
337 		if (!strcmp(ops->kind, kind))
338 			return ops;
339 	}
340 	return NULL;
341 }
342 
343 static size_t rtnl_link_get_size(const struct net_device *dev)
344 {
345 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
346 	size_t size;
347 
348 	if (!ops)
349 		return 0;
350 
351 	size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
352 	       nlmsg_total_size(strlen(ops->kind) + 1);	 /* IFLA_INFO_KIND */
353 
354 	if (ops->get_size)
355 		/* IFLA_INFO_DATA + nested data */
356 		size += nlmsg_total_size(sizeof(struct nlattr)) +
357 			ops->get_size(dev);
358 
359 	if (ops->get_xstats_size)
360 		size += ops->get_xstats_size(dev);	/* IFLA_INFO_XSTATS */
361 
362 	return size;
363 }
364 
365 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
366 {
367 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
368 	struct nlattr *linkinfo, *data;
369 	int err = -EMSGSIZE;
370 
371 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
372 	if (linkinfo == NULL)
373 		goto out;
374 
375 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
376 		goto err_cancel_link;
377 	if (ops->fill_xstats) {
378 		err = ops->fill_xstats(skb, dev);
379 		if (err < 0)
380 			goto err_cancel_link;
381 	}
382 	if (ops->fill_info) {
383 		data = nla_nest_start(skb, IFLA_INFO_DATA);
384 		if (data == NULL)
385 			goto err_cancel_link;
386 		err = ops->fill_info(skb, dev);
387 		if (err < 0)
388 			goto err_cancel_data;
389 		nla_nest_end(skb, data);
390 	}
391 
392 	nla_nest_end(skb, linkinfo);
393 	return 0;
394 
395 err_cancel_data:
396 	nla_nest_cancel(skb, data);
397 err_cancel_link:
398 	nla_nest_cancel(skb, linkinfo);
399 out:
400 	return err;
401 }
402 
403 static const int rtm_min[RTM_NR_FAMILIES] =
404 {
405 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
406 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
407 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
408 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
409 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
410 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
411 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
412 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
413 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
414 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
415 };
416 
417 static const int rta_max[RTM_NR_FAMILIES] =
418 {
419 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
420 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
421 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
422 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
423 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
424 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
425 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
426 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
427 };
428 
429 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
430 {
431 	struct rtattr *rta;
432 	int size = RTA_LENGTH(attrlen);
433 
434 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
435 	rta->rta_type = attrtype;
436 	rta->rta_len = size;
437 	memcpy(RTA_DATA(rta), data, attrlen);
438 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
439 }
440 
441 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
442 {
443 	size_t ret = RTA_PAYLOAD(rta);
444 	char *src = RTA_DATA(rta);
445 
446 	if (ret > 0 && src[ret - 1] == '\0')
447 		ret--;
448 	if (size > 0) {
449 		size_t len = (ret >= size) ? size - 1 : ret;
450 		memset(dest, 0, size);
451 		memcpy(dest, src, len);
452 	}
453 	return ret;
454 }
455 
456 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
457 {
458 	int err = 0;
459 
460 	NETLINK_CB(skb).dst_group = group;
461 	if (echo)
462 		atomic_inc(&skb->users);
463 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
464 	if (echo)
465 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
466 	return err;
467 }
468 
469 int rtnl_unicast(struct sk_buff *skb, u32 pid)
470 {
471 	return nlmsg_unicast(rtnl, skb, pid);
472 }
473 
474 int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
475 		struct nlmsghdr *nlh, gfp_t flags)
476 {
477 	int report = 0;
478 
479 	if (nlh)
480 		report = nlmsg_report(nlh);
481 
482 	return nlmsg_notify(rtnl, skb, pid, group, report, flags);
483 }
484 
485 void rtnl_set_sk_err(u32 group, int error)
486 {
487 	netlink_set_err(rtnl, 0, group, error);
488 }
489 
490 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
491 {
492 	struct nlattr *mx;
493 	int i, valid = 0;
494 
495 	mx = nla_nest_start(skb, RTA_METRICS);
496 	if (mx == NULL)
497 		return -ENOBUFS;
498 
499 	for (i = 0; i < RTAX_MAX; i++) {
500 		if (metrics[i]) {
501 			valid++;
502 			NLA_PUT_U32(skb, i+1, metrics[i]);
503 		}
504 	}
505 
506 	if (!valid) {
507 		nla_nest_cancel(skb, mx);
508 		return 0;
509 	}
510 
511 	return nla_nest_end(skb, mx);
512 
513 nla_put_failure:
514 	return nla_nest_cancel(skb, mx);
515 }
516 
517 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
518 		       u32 ts, u32 tsage, long expires, u32 error)
519 {
520 	struct rta_cacheinfo ci = {
521 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
522 		.rta_used = dst->__use,
523 		.rta_clntref = atomic_read(&(dst->__refcnt)),
524 		.rta_error = error,
525 		.rta_id =  id,
526 		.rta_ts = ts,
527 		.rta_tsage = tsage,
528 	};
529 
530 	if (expires)
531 		ci.rta_expires = jiffies_to_clock_t(expires);
532 
533 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
534 }
535 
536 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
537 
538 static void set_operstate(struct net_device *dev, unsigned char transition)
539 {
540 	unsigned char operstate = dev->operstate;
541 
542 	switch(transition) {
543 	case IF_OPER_UP:
544 		if ((operstate == IF_OPER_DORMANT ||
545 		     operstate == IF_OPER_UNKNOWN) &&
546 		    !netif_dormant(dev))
547 			operstate = IF_OPER_UP;
548 		break;
549 
550 	case IF_OPER_DORMANT:
551 		if (operstate == IF_OPER_UP ||
552 		    operstate == IF_OPER_UNKNOWN)
553 			operstate = IF_OPER_DORMANT;
554 		break;
555 	}
556 
557 	if (dev->operstate != operstate) {
558 		write_lock_bh(&dev_base_lock);
559 		dev->operstate = operstate;
560 		write_unlock_bh(&dev_base_lock);
561 		netdev_state_change(dev);
562 	}
563 }
564 
565 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
566 				 struct net_device_stats *b)
567 {
568 	a->rx_packets = b->rx_packets;
569 	a->tx_packets = b->tx_packets;
570 	a->rx_bytes = b->rx_bytes;
571 	a->tx_bytes = b->tx_bytes;
572 	a->rx_errors = b->rx_errors;
573 	a->tx_errors = b->tx_errors;
574 	a->rx_dropped = b->rx_dropped;
575 	a->tx_dropped = b->tx_dropped;
576 
577 	a->multicast = b->multicast;
578 	a->collisions = b->collisions;
579 
580 	a->rx_length_errors = b->rx_length_errors;
581 	a->rx_over_errors = b->rx_over_errors;
582 	a->rx_crc_errors = b->rx_crc_errors;
583 	a->rx_frame_errors = b->rx_frame_errors;
584 	a->rx_fifo_errors = b->rx_fifo_errors;
585 	a->rx_missed_errors = b->rx_missed_errors;
586 
587 	a->tx_aborted_errors = b->tx_aborted_errors;
588 	a->tx_carrier_errors = b->tx_carrier_errors;
589 	a->tx_fifo_errors = b->tx_fifo_errors;
590 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
591 	a->tx_window_errors = b->tx_window_errors;
592 
593 	a->rx_compressed = b->rx_compressed;
594 	a->tx_compressed = b->tx_compressed;
595 };
596 
597 static inline size_t if_nlmsg_size(const struct net_device *dev)
598 {
599 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
600 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
601 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
602 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
603 	       + nla_total_size(sizeof(struct rtnl_link_stats))
604 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
605 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
606 	       + nla_total_size(4) /* IFLA_TXQLEN */
607 	       + nla_total_size(4) /* IFLA_WEIGHT */
608 	       + nla_total_size(4) /* IFLA_MTU */
609 	       + nla_total_size(4) /* IFLA_LINK */
610 	       + nla_total_size(4) /* IFLA_MASTER */
611 	       + nla_total_size(1) /* IFLA_OPERSTATE */
612 	       + nla_total_size(1) /* IFLA_LINKMODE */
613 	       + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
614 }
615 
616 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
617 			    int type, u32 pid, u32 seq, u32 change,
618 			    unsigned int flags)
619 {
620 	struct ifinfomsg *ifm;
621 	struct nlmsghdr *nlh;
622 
623 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
624 	if (nlh == NULL)
625 		return -EMSGSIZE;
626 
627 	ifm = nlmsg_data(nlh);
628 	ifm->ifi_family = AF_UNSPEC;
629 	ifm->__ifi_pad = 0;
630 	ifm->ifi_type = dev->type;
631 	ifm->ifi_index = dev->ifindex;
632 	ifm->ifi_flags = dev_get_flags(dev);
633 	ifm->ifi_change = change;
634 
635 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
636 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
637 	NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
638 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
639 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
640 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
641 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
642 
643 	if (dev->ifindex != dev->iflink)
644 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
645 
646 	if (dev->master)
647 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
648 
649 	if (dev->qdisc_sleeping)
650 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
651 
652 	if (1) {
653 		struct rtnl_link_ifmap map = {
654 			.mem_start   = dev->mem_start,
655 			.mem_end     = dev->mem_end,
656 			.base_addr   = dev->base_addr,
657 			.irq         = dev->irq,
658 			.dma         = dev->dma,
659 			.port        = dev->if_port,
660 		};
661 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
662 	}
663 
664 	if (dev->addr_len) {
665 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
666 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
667 	}
668 
669 	if (dev->get_stats) {
670 		struct net_device_stats *stats = dev->get_stats(dev);
671 		if (stats) {
672 			struct nlattr *attr;
673 
674 			attr = nla_reserve(skb, IFLA_STATS,
675 					   sizeof(struct rtnl_link_stats));
676 			if (attr == NULL)
677 				goto nla_put_failure;
678 
679 			copy_rtnl_link_stats(nla_data(attr), stats);
680 		}
681 	}
682 
683 	if (dev->rtnl_link_ops) {
684 		if (rtnl_link_fill(skb, dev) < 0)
685 			goto nla_put_failure;
686 	}
687 
688 	return nlmsg_end(skb, nlh);
689 
690 nla_put_failure:
691 	nlmsg_cancel(skb, nlh);
692 	return -EMSGSIZE;
693 }
694 
695 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
696 {
697 	int idx;
698 	int s_idx = cb->args[0];
699 	struct net_device *dev;
700 
701 	idx = 0;
702 	for_each_netdev(dev) {
703 		if (idx < s_idx)
704 			goto cont;
705 		if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
706 				     NETLINK_CB(cb->skb).pid,
707 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
708 			break;
709 cont:
710 		idx++;
711 	}
712 	cb->args[0] = idx;
713 
714 	return skb->len;
715 }
716 
717 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
718 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
719 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
720 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
721 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
722 	[IFLA_MTU]		= { .type = NLA_U32 },
723 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
724 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
725 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
726 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
727 };
728 
729 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
730 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
731 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
732 };
733 
734 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
735 		      struct nlattr **tb, char *ifname, int modified)
736 {
737 	int send_addr_notify = 0;
738 	int err;
739 
740 	if (tb[IFLA_MAP]) {
741 		struct rtnl_link_ifmap *u_map;
742 		struct ifmap k_map;
743 
744 		if (!dev->set_config) {
745 			err = -EOPNOTSUPP;
746 			goto errout;
747 		}
748 
749 		if (!netif_device_present(dev)) {
750 			err = -ENODEV;
751 			goto errout;
752 		}
753 
754 		u_map = nla_data(tb[IFLA_MAP]);
755 		k_map.mem_start = (unsigned long) u_map->mem_start;
756 		k_map.mem_end = (unsigned long) u_map->mem_end;
757 		k_map.base_addr = (unsigned short) u_map->base_addr;
758 		k_map.irq = (unsigned char) u_map->irq;
759 		k_map.dma = (unsigned char) u_map->dma;
760 		k_map.port = (unsigned char) u_map->port;
761 
762 		err = dev->set_config(dev, &k_map);
763 		if (err < 0)
764 			goto errout;
765 
766 		modified = 1;
767 	}
768 
769 	if (tb[IFLA_ADDRESS]) {
770 		struct sockaddr *sa;
771 		int len;
772 
773 		if (!dev->set_mac_address) {
774 			err = -EOPNOTSUPP;
775 			goto errout;
776 		}
777 
778 		if (!netif_device_present(dev)) {
779 			err = -ENODEV;
780 			goto errout;
781 		}
782 
783 		len = sizeof(sa_family_t) + dev->addr_len;
784 		sa = kmalloc(len, GFP_KERNEL);
785 		if (!sa) {
786 			err = -ENOMEM;
787 			goto errout;
788 		}
789 		sa->sa_family = dev->type;
790 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
791 		       dev->addr_len);
792 		err = dev->set_mac_address(dev, sa);
793 		kfree(sa);
794 		if (err)
795 			goto errout;
796 		send_addr_notify = 1;
797 		modified = 1;
798 	}
799 
800 	if (tb[IFLA_MTU]) {
801 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
802 		if (err < 0)
803 			goto errout;
804 		modified = 1;
805 	}
806 
807 	/*
808 	 * Interface selected by interface index but interface
809 	 * name provided implies that a name change has been
810 	 * requested.
811 	 */
812 	if (ifm->ifi_index > 0 && ifname[0]) {
813 		err = dev_change_name(dev, ifname);
814 		if (err < 0)
815 			goto errout;
816 		modified = 1;
817 	}
818 
819 	if (tb[IFLA_BROADCAST]) {
820 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
821 		send_addr_notify = 1;
822 	}
823 
824 	if (ifm->ifi_flags || ifm->ifi_change) {
825 		unsigned int flags = ifm->ifi_flags;
826 
827 		/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
828 		if (ifm->ifi_change)
829 			flags = (flags & ifm->ifi_change) |
830 				(dev->flags & ~ifm->ifi_change);
831 		dev_change_flags(dev, flags);
832 	}
833 
834 	if (tb[IFLA_TXQLEN])
835 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
836 
837 	if (tb[IFLA_WEIGHT])
838 		dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
839 
840 	if (tb[IFLA_OPERSTATE])
841 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
842 
843 	if (tb[IFLA_LINKMODE]) {
844 		write_lock_bh(&dev_base_lock);
845 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
846 		write_unlock_bh(&dev_base_lock);
847 	}
848 
849 	err = 0;
850 
851 errout:
852 	if (err < 0 && modified && net_ratelimit())
853 		printk(KERN_WARNING "A link change request failed with "
854 		       "some changes comitted already. Interface %s may "
855 		       "have been left with an inconsistent configuration, "
856 		       "please check.\n", dev->name);
857 
858 	if (send_addr_notify)
859 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
860 	return err;
861 }
862 
863 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
864 {
865 	struct ifinfomsg *ifm;
866 	struct net_device *dev;
867 	int err;
868 	struct nlattr *tb[IFLA_MAX+1];
869 	char ifname[IFNAMSIZ];
870 
871 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
872 	if (err < 0)
873 		goto errout;
874 
875 	if (tb[IFLA_IFNAME])
876 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
877 	else
878 		ifname[0] = '\0';
879 
880 	err = -EINVAL;
881 	ifm = nlmsg_data(nlh);
882 	if (ifm->ifi_index > 0)
883 		dev = dev_get_by_index(ifm->ifi_index);
884 	else if (tb[IFLA_IFNAME])
885 		dev = dev_get_by_name(ifname);
886 	else
887 		goto errout;
888 
889 	if (dev == NULL) {
890 		err = -ENODEV;
891 		goto errout;
892 	}
893 
894 	if (tb[IFLA_ADDRESS] &&
895 	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
896 		goto errout_dev;
897 
898 	if (tb[IFLA_BROADCAST] &&
899 	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
900 		goto errout_dev;
901 
902 	err = do_setlink(dev, ifm, tb, ifname, 0);
903 errout_dev:
904 	dev_put(dev);
905 errout:
906 	return err;
907 }
908 
909 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
910 {
911 	const struct rtnl_link_ops *ops;
912 	struct net_device *dev;
913 	struct ifinfomsg *ifm;
914 	char ifname[IFNAMSIZ];
915 	struct nlattr *tb[IFLA_MAX+1];
916 	int err;
917 
918 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
919 	if (err < 0)
920 		return err;
921 
922 	if (tb[IFLA_IFNAME])
923 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
924 
925 	ifm = nlmsg_data(nlh);
926 	if (ifm->ifi_index > 0)
927 		dev = __dev_get_by_index(ifm->ifi_index);
928 	else if (tb[IFLA_IFNAME])
929 		dev = __dev_get_by_name(ifname);
930 	else
931 		return -EINVAL;
932 
933 	if (!dev)
934 		return -ENODEV;
935 
936 	ops = dev->rtnl_link_ops;
937 	if (!ops)
938 		return -EOPNOTSUPP;
939 
940 	ops->dellink(dev);
941 	return 0;
942 }
943 
944 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
945 {
946 	const struct rtnl_link_ops *ops;
947 	struct net_device *dev;
948 	struct ifinfomsg *ifm;
949 	char kind[MODULE_NAME_LEN];
950 	char ifname[IFNAMSIZ];
951 	struct nlattr *tb[IFLA_MAX+1];
952 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
953 	int err;
954 
955 replay:
956 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
957 	if (err < 0)
958 		return err;
959 
960 	if (tb[IFLA_IFNAME])
961 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
962 	else
963 		ifname[0] = '\0';
964 
965 	ifm = nlmsg_data(nlh);
966 	if (ifm->ifi_index > 0)
967 		dev = __dev_get_by_index(ifm->ifi_index);
968 	else if (ifname[0])
969 		dev = __dev_get_by_name(ifname);
970 	else
971 		dev = NULL;
972 
973 	if (tb[IFLA_LINKINFO]) {
974 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
975 				       tb[IFLA_LINKINFO], ifla_info_policy);
976 		if (err < 0)
977 			return err;
978 	} else
979 		memset(linkinfo, 0, sizeof(linkinfo));
980 
981 	if (linkinfo[IFLA_INFO_KIND]) {
982 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
983 		ops = rtnl_link_ops_get(kind);
984 	} else {
985 		kind[0] = '\0';
986 		ops = NULL;
987 	}
988 
989 	if (1) {
990 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
991 
992 		if (ops) {
993 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
994 				err = nla_parse_nested(attr, ops->maxtype,
995 						       linkinfo[IFLA_INFO_DATA],
996 						       ops->policy);
997 				if (err < 0)
998 					return err;
999 				data = attr;
1000 			}
1001 			if (ops->validate) {
1002 				err = ops->validate(tb, data);
1003 				if (err < 0)
1004 					return err;
1005 			}
1006 		}
1007 
1008 		if (dev) {
1009 			int modified = 0;
1010 
1011 			if (nlh->nlmsg_flags & NLM_F_EXCL)
1012 				return -EEXIST;
1013 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
1014 				return -EOPNOTSUPP;
1015 
1016 			if (linkinfo[IFLA_INFO_DATA]) {
1017 				if (!ops || ops != dev->rtnl_link_ops ||
1018 				    !ops->changelink)
1019 					return -EOPNOTSUPP;
1020 
1021 				err = ops->changelink(dev, tb, data);
1022 				if (err < 0)
1023 					return err;
1024 				modified = 1;
1025 			}
1026 
1027 			return do_setlink(dev, ifm, tb, ifname, modified);
1028 		}
1029 
1030 		if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1031 			return -ENODEV;
1032 
1033 		if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1034 			return -EOPNOTSUPP;
1035 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1036 			return -EOPNOTSUPP;
1037 
1038 		if (!ops) {
1039 #ifdef CONFIG_KMOD
1040 			if (kind[0]) {
1041 				__rtnl_unlock();
1042 				request_module("rtnl-link-%s", kind);
1043 				rtnl_lock();
1044 				ops = rtnl_link_ops_get(kind);
1045 				if (ops)
1046 					goto replay;
1047 			}
1048 #endif
1049 			return -EOPNOTSUPP;
1050 		}
1051 
1052 		if (!ifname[0])
1053 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1054 		dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
1055 		if (!dev)
1056 			return -ENOMEM;
1057 
1058 		if (strchr(dev->name, '%')) {
1059 			err = dev_alloc_name(dev, dev->name);
1060 			if (err < 0)
1061 				goto err_free;
1062 		}
1063 		dev->rtnl_link_ops = ops;
1064 
1065 		if (tb[IFLA_MTU])
1066 			dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1067 		if (tb[IFLA_ADDRESS])
1068 			memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1069 			       nla_len(tb[IFLA_ADDRESS]));
1070 		if (tb[IFLA_BROADCAST])
1071 			memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1072 			       nla_len(tb[IFLA_BROADCAST]));
1073 		if (tb[IFLA_TXQLEN])
1074 			dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1075 		if (tb[IFLA_WEIGHT])
1076 			dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
1077 		if (tb[IFLA_OPERSTATE])
1078 			set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1079 		if (tb[IFLA_LINKMODE])
1080 			dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1081 
1082 		if (ops->newlink)
1083 			err = ops->newlink(dev, tb, data);
1084 		else
1085 			err = register_netdevice(dev);
1086 err_free:
1087 		if (err < 0)
1088 			free_netdev(dev);
1089 		return err;
1090 	}
1091 }
1092 
1093 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1094 {
1095 	struct ifinfomsg *ifm;
1096 	struct nlattr *tb[IFLA_MAX+1];
1097 	struct net_device *dev = NULL;
1098 	struct sk_buff *nskb;
1099 	int err;
1100 
1101 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1102 	if (err < 0)
1103 		return err;
1104 
1105 	ifm = nlmsg_data(nlh);
1106 	if (ifm->ifi_index > 0) {
1107 		dev = dev_get_by_index(ifm->ifi_index);
1108 		if (dev == NULL)
1109 			return -ENODEV;
1110 	} else
1111 		return -EINVAL;
1112 
1113 	nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1114 	if (nskb == NULL) {
1115 		err = -ENOBUFS;
1116 		goto errout;
1117 	}
1118 
1119 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1120 			       nlh->nlmsg_seq, 0, 0);
1121 	if (err < 0) {
1122 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
1123 		WARN_ON(err == -EMSGSIZE);
1124 		kfree_skb(nskb);
1125 		goto errout;
1126 	}
1127 	err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
1128 errout:
1129 	dev_put(dev);
1130 
1131 	return err;
1132 }
1133 
1134 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1135 {
1136 	int idx;
1137 	int s_idx = cb->family;
1138 
1139 	if (s_idx == 0)
1140 		s_idx = 1;
1141 	for (idx=1; idx<NPROTO; idx++) {
1142 		int type = cb->nlh->nlmsg_type-RTM_BASE;
1143 		if (idx < s_idx || idx == PF_PACKET)
1144 			continue;
1145 		if (rtnl_msg_handlers[idx] == NULL ||
1146 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
1147 			continue;
1148 		if (idx > s_idx)
1149 			memset(&cb->args[0], 0, sizeof(cb->args));
1150 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1151 			break;
1152 	}
1153 	cb->family = idx;
1154 
1155 	return skb->len;
1156 }
1157 
1158 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1159 {
1160 	struct sk_buff *skb;
1161 	int err = -ENOBUFS;
1162 
1163 	skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1164 	if (skb == NULL)
1165 		goto errout;
1166 
1167 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
1168 	if (err < 0) {
1169 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
1170 		WARN_ON(err == -EMSGSIZE);
1171 		kfree_skb(skb);
1172 		goto errout;
1173 	}
1174 	err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1175 errout:
1176 	if (err < 0)
1177 		rtnl_set_sk_err(RTNLGRP_LINK, err);
1178 }
1179 
1180 /* Protected by RTNL sempahore.  */
1181 static struct rtattr **rta_buf;
1182 static int rtattr_max;
1183 
1184 /* Process one rtnetlink message. */
1185 
1186 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1187 {
1188 	rtnl_doit_func doit;
1189 	int sz_idx, kind;
1190 	int min_len;
1191 	int family;
1192 	int type;
1193 	int err;
1194 
1195 	type = nlh->nlmsg_type;
1196 	if (type > RTM_MAX)
1197 		return -EOPNOTSUPP;
1198 
1199 	type -= RTM_BASE;
1200 
1201 	/* All the messages must have at least 1 byte length */
1202 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1203 		return 0;
1204 
1205 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
1206 	if (family >= NPROTO)
1207 		return -EAFNOSUPPORT;
1208 
1209 	sz_idx = type>>2;
1210 	kind = type&3;
1211 
1212 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1213 		return -EPERM;
1214 
1215 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1216 		rtnl_dumpit_func dumpit;
1217 
1218 		dumpit = rtnl_get_dumpit(family, type);
1219 		if (dumpit == NULL)
1220 			return -EOPNOTSUPP;
1221 
1222 		__rtnl_unlock();
1223 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1224 		rtnl_lock();
1225 		return err;
1226 	}
1227 
1228 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1229 
1230 	min_len = rtm_min[sz_idx];
1231 	if (nlh->nlmsg_len < min_len)
1232 		return -EINVAL;
1233 
1234 	if (nlh->nlmsg_len > min_len) {
1235 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1236 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
1237 
1238 		while (RTA_OK(attr, attrlen)) {
1239 			unsigned flavor = attr->rta_type;
1240 			if (flavor) {
1241 				if (flavor > rta_max[sz_idx])
1242 					return -EINVAL;
1243 				rta_buf[flavor-1] = attr;
1244 			}
1245 			attr = RTA_NEXT(attr, attrlen);
1246 		}
1247 	}
1248 
1249 	doit = rtnl_get_doit(family, type);
1250 	if (doit == NULL)
1251 		return -EOPNOTSUPP;
1252 
1253 	return doit(skb, nlh, (void *)&rta_buf[0]);
1254 }
1255 
1256 static void rtnetlink_rcv(struct sock *sk, int len)
1257 {
1258 	unsigned int qlen = 0;
1259 
1260 	do {
1261 		mutex_lock(&rtnl_mutex);
1262 		netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
1263 		mutex_unlock(&rtnl_mutex);
1264 
1265 		netdev_run_todo();
1266 	} while (qlen);
1267 }
1268 
1269 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1270 {
1271 	struct net_device *dev = ptr;
1272 	switch (event) {
1273 	case NETDEV_UNREGISTER:
1274 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1275 		break;
1276 	case NETDEV_REGISTER:
1277 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1278 		break;
1279 	case NETDEV_UP:
1280 	case NETDEV_DOWN:
1281 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1282 		break;
1283 	case NETDEV_CHANGE:
1284 	case NETDEV_GOING_DOWN:
1285 		break;
1286 	default:
1287 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1288 		break;
1289 	}
1290 	return NOTIFY_DONE;
1291 }
1292 
1293 static struct notifier_block rtnetlink_dev_notifier = {
1294 	.notifier_call	= rtnetlink_event,
1295 };
1296 
1297 void __init rtnetlink_init(void)
1298 {
1299 	int i;
1300 
1301 	rtattr_max = 0;
1302 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1303 		if (rta_max[i] > rtattr_max)
1304 			rtattr_max = rta_max[i];
1305 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1306 	if (!rta_buf)
1307 		panic("rtnetlink_init: cannot allocate rta_buf\n");
1308 
1309 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
1310 				     &rtnl_mutex, THIS_MODULE);
1311 	if (rtnl == NULL)
1312 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
1313 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1314 	register_netdevice_notifier(&rtnetlink_dev_notifier);
1315 
1316 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1317 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
1318 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1319 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1320 
1321 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1322 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
1323 }
1324 
1325 EXPORT_SYMBOL(__rta_fill);
1326 EXPORT_SYMBOL(rtattr_strlcpy);
1327 EXPORT_SYMBOL(rtattr_parse);
1328 EXPORT_SYMBOL(__rtattr_parse_nested_compat);
1329 EXPORT_SYMBOL(rtnetlink_put_metrics);
1330 EXPORT_SYMBOL(rtnl_lock);
1331 EXPORT_SYMBOL(rtnl_trylock);
1332 EXPORT_SYMBOL(rtnl_unlock);
1333 EXPORT_SYMBOL(rtnl_unicast);
1334 EXPORT_SYMBOL(rtnl_notify);
1335 EXPORT_SYMBOL(rtnl_set_sk_err);
1336