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.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
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>
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 #include <linux/pci.h>
40 #include <asm/uaccess.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
45 #include <net/protocol.h>
47 #include <net/route.h>
50 #include <net/pkt_sched.h>
51 #include <net/fib_rules.h>
52 #include <net/rtnetlink.h>
53 #include <net/net_namespace.h>
57 rtnl_dumpit_func dumpit
;
58 rtnl_calcit_func calcit
;
61 static DEFINE_MUTEX(rtnl_mutex
);
65 mutex_lock(&rtnl_mutex
);
67 EXPORT_SYMBOL(rtnl_lock
);
69 void __rtnl_unlock(void)
71 mutex_unlock(&rtnl_mutex
);
74 void rtnl_unlock(void)
76 /* This fellow will unlock it for us. */
79 EXPORT_SYMBOL(rtnl_unlock
);
81 int rtnl_trylock(void)
83 return mutex_trylock(&rtnl_mutex
);
85 EXPORT_SYMBOL(rtnl_trylock
);
87 int rtnl_is_locked(void)
89 return mutex_is_locked(&rtnl_mutex
);
91 EXPORT_SYMBOL(rtnl_is_locked
);
93 #ifdef CONFIG_PROVE_LOCKING
94 int lockdep_rtnl_is_held(void)
96 return lockdep_is_held(&rtnl_mutex
);
98 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
99 #endif /* #ifdef CONFIG_PROVE_LOCKING */
101 static struct rtnl_link
*rtnl_msg_handlers
[RTNL_FAMILY_MAX
+ 1];
103 static inline int rtm_msgindex(int msgtype
)
105 int msgindex
= msgtype
- RTM_BASE
;
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
112 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
117 static rtnl_doit_func
rtnl_get_doit(int protocol
, int msgindex
)
119 struct rtnl_link
*tab
;
121 if (protocol
<= RTNL_FAMILY_MAX
)
122 tab
= rtnl_msg_handlers
[protocol
];
126 if (tab
== NULL
|| tab
[msgindex
].doit
== NULL
)
127 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
129 return tab
? tab
[msgindex
].doit
: NULL
;
132 static rtnl_dumpit_func
rtnl_get_dumpit(int protocol
, int msgindex
)
134 struct rtnl_link
*tab
;
136 if (protocol
<= RTNL_FAMILY_MAX
)
137 tab
= rtnl_msg_handlers
[protocol
];
141 if (tab
== NULL
|| tab
[msgindex
].dumpit
== NULL
)
142 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
144 return tab
? tab
[msgindex
].dumpit
: NULL
;
147 static rtnl_calcit_func
rtnl_get_calcit(int protocol
, int msgindex
)
149 struct rtnl_link
*tab
;
151 if (protocol
<= RTNL_FAMILY_MAX
)
152 tab
= rtnl_msg_handlers
[protocol
];
156 if (tab
== NULL
|| tab
[msgindex
].calcit
== NULL
)
157 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
159 return tab
? tab
[msgindex
].calcit
: NULL
;
163 * __rtnl_register - Register a rtnetlink message type
164 * @protocol: Protocol family or PF_UNSPEC
165 * @msgtype: rtnetlink message type
166 * @doit: Function pointer called for each request message
167 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
168 * @calcit: Function pointer to calc size of dump message
170 * Registers the specified function pointers (at least one of them has
171 * to be non-NULL) to be called whenever a request message for the
172 * specified protocol family and message type is received.
174 * The special protocol family PF_UNSPEC may be used to define fallback
175 * function pointers for the case when no entry for the specific protocol
178 * Returns 0 on success or a negative error code.
180 int __rtnl_register(int protocol
, int msgtype
,
181 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
182 rtnl_calcit_func calcit
)
184 struct rtnl_link
*tab
;
187 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
188 msgindex
= rtm_msgindex(msgtype
);
190 tab
= rtnl_msg_handlers
[protocol
];
192 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
196 rtnl_msg_handlers
[protocol
] = tab
;
200 tab
[msgindex
].doit
= doit
;
203 tab
[msgindex
].dumpit
= dumpit
;
206 tab
[msgindex
].calcit
= calcit
;
210 EXPORT_SYMBOL_GPL(__rtnl_register
);
213 * rtnl_register - Register a rtnetlink message type
215 * Identical to __rtnl_register() but panics on failure. This is useful
216 * as failure of this function is very unlikely, it can only happen due
217 * to lack of memory when allocating the chain to store all message
218 * handlers for a protocol. Meant for use in init functions where lack
219 * of memory implies no sense in continuing.
221 void rtnl_register(int protocol
, int msgtype
,
222 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
,
223 rtnl_calcit_func calcit
)
225 if (__rtnl_register(protocol
, msgtype
, doit
, dumpit
, calcit
) < 0)
226 panic("Unable to register rtnetlink message handler, "
227 "protocol = %d, message type = %d\n",
230 EXPORT_SYMBOL_GPL(rtnl_register
);
233 * rtnl_unregister - Unregister a rtnetlink message type
234 * @protocol: Protocol family or PF_UNSPEC
235 * @msgtype: rtnetlink message type
237 * Returns 0 on success or a negative error code.
239 int rtnl_unregister(int protocol
, int msgtype
)
243 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
244 msgindex
= rtm_msgindex(msgtype
);
246 if (rtnl_msg_handlers
[protocol
] == NULL
)
249 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
250 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
254 EXPORT_SYMBOL_GPL(rtnl_unregister
);
257 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
258 * @protocol : Protocol family or PF_UNSPEC
260 * Identical to calling rtnl_unregster() for all registered message types
261 * of a certain protocol family.
263 void rtnl_unregister_all(int protocol
)
265 BUG_ON(protocol
< 0 || protocol
> RTNL_FAMILY_MAX
);
267 kfree(rtnl_msg_handlers
[protocol
]);
268 rtnl_msg_handlers
[protocol
] = NULL
;
270 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
272 static LIST_HEAD(link_ops
);
274 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
276 const struct rtnl_link_ops
*ops
;
278 list_for_each_entry(ops
, &link_ops
, list
) {
279 if (!strcmp(ops
->kind
, kind
))
286 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
287 * @ops: struct rtnl_link_ops * to register
289 * The caller must hold the rtnl_mutex. This function should be used
290 * by drivers that create devices during module initialization. It
291 * must be called before registering the devices.
293 * Returns 0 on success or a negative error code.
295 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
297 if (rtnl_link_ops_get(ops
->kind
))
301 ops
->dellink
= unregister_netdevice_queue
;
303 list_add_tail(&ops
->list
, &link_ops
);
306 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
309 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
310 * @ops: struct rtnl_link_ops * to register
312 * Returns 0 on success or a negative error code.
314 int rtnl_link_register(struct rtnl_link_ops
*ops
)
319 err
= __rtnl_link_register(ops
);
323 EXPORT_SYMBOL_GPL(rtnl_link_register
);
325 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
327 struct net_device
*dev
;
328 LIST_HEAD(list_kill
);
330 for_each_netdev(net
, dev
) {
331 if (dev
->rtnl_link_ops
== ops
)
332 ops
->dellink(dev
, &list_kill
);
334 unregister_netdevice_many(&list_kill
);
338 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
339 * @ops: struct rtnl_link_ops * to unregister
341 * The caller must hold the rtnl_mutex.
343 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
348 __rtnl_kill_links(net
, ops
);
350 list_del(&ops
->list
);
352 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
355 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
356 * @ops: struct rtnl_link_ops * to unregister
358 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
361 __rtnl_link_unregister(ops
);
364 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
366 static size_t rtnl_link_get_size(const struct net_device
*dev
)
368 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
374 size
= nla_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
375 nla_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
378 /* IFLA_INFO_DATA + nested data */
379 size
+= nla_total_size(sizeof(struct nlattr
)) +
382 if (ops
->get_xstats_size
)
383 /* IFLA_INFO_XSTATS */
384 size
+= nla_total_size(ops
->get_xstats_size(dev
));
389 static LIST_HEAD(rtnl_af_ops
);
391 static const struct rtnl_af_ops
*rtnl_af_lookup(const int family
)
393 const struct rtnl_af_ops
*ops
;
395 list_for_each_entry(ops
, &rtnl_af_ops
, list
) {
396 if (ops
->family
== family
)
404 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
405 * @ops: struct rtnl_af_ops * to register
407 * The caller must hold the rtnl_mutex.
409 * Returns 0 on success or a negative error code.
411 int __rtnl_af_register(struct rtnl_af_ops
*ops
)
413 list_add_tail(&ops
->list
, &rtnl_af_ops
);
416 EXPORT_SYMBOL_GPL(__rtnl_af_register
);
419 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
420 * @ops: struct rtnl_af_ops * to register
422 * Returns 0 on success or a negative error code.
424 int rtnl_af_register(struct rtnl_af_ops
*ops
)
429 err
= __rtnl_af_register(ops
);
433 EXPORT_SYMBOL_GPL(rtnl_af_register
);
436 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
437 * @ops: struct rtnl_af_ops * to unregister
439 * The caller must hold the rtnl_mutex.
441 void __rtnl_af_unregister(struct rtnl_af_ops
*ops
)
443 list_del(&ops
->list
);
445 EXPORT_SYMBOL_GPL(__rtnl_af_unregister
);
448 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
449 * @ops: struct rtnl_af_ops * to unregister
451 void rtnl_af_unregister(struct rtnl_af_ops
*ops
)
454 __rtnl_af_unregister(ops
);
457 EXPORT_SYMBOL_GPL(rtnl_af_unregister
);
459 static size_t rtnl_link_get_af_size(const struct net_device
*dev
)
461 struct rtnl_af_ops
*af_ops
;
465 size
= nla_total_size(sizeof(struct nlattr
));
467 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
468 if (af_ops
->get_link_af_size
) {
469 /* AF_* + nested data */
470 size
+= nla_total_size(sizeof(struct nlattr
)) +
471 af_ops
->get_link_af_size(dev
);
478 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
480 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
481 struct nlattr
*linkinfo
, *data
;
484 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
485 if (linkinfo
== NULL
)
488 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
489 goto err_cancel_link
;
490 if (ops
->fill_xstats
) {
491 err
= ops
->fill_xstats(skb
, dev
);
493 goto err_cancel_link
;
495 if (ops
->fill_info
) {
496 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
498 goto err_cancel_link
;
499 err
= ops
->fill_info(skb
, dev
);
501 goto err_cancel_data
;
502 nla_nest_end(skb
, data
);
505 nla_nest_end(skb
, linkinfo
);
509 nla_nest_cancel(skb
, data
);
511 nla_nest_cancel(skb
, linkinfo
);
516 static const int rtm_min
[RTM_NR_FAMILIES
] =
518 [RTM_FAM(RTM_NEWLINK
)] = NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
519 [RTM_FAM(RTM_NEWADDR
)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
520 [RTM_FAM(RTM_NEWROUTE
)] = NLMSG_LENGTH(sizeof(struct rtmsg
)),
521 [RTM_FAM(RTM_NEWRULE
)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
522 [RTM_FAM(RTM_NEWQDISC
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
523 [RTM_FAM(RTM_NEWTCLASS
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
524 [RTM_FAM(RTM_NEWTFILTER
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
525 [RTM_FAM(RTM_NEWACTION
)] = NLMSG_LENGTH(sizeof(struct tcamsg
)),
526 [RTM_FAM(RTM_GETMULTICAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
527 [RTM_FAM(RTM_GETANYCAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
530 static const int rta_max
[RTM_NR_FAMILIES
] =
532 [RTM_FAM(RTM_NEWLINK
)] = IFLA_MAX
,
533 [RTM_FAM(RTM_NEWADDR
)] = IFA_MAX
,
534 [RTM_FAM(RTM_NEWROUTE
)] = RTA_MAX
,
535 [RTM_FAM(RTM_NEWRULE
)] = FRA_MAX
,
536 [RTM_FAM(RTM_NEWQDISC
)] = TCA_MAX
,
537 [RTM_FAM(RTM_NEWTCLASS
)] = TCA_MAX
,
538 [RTM_FAM(RTM_NEWTFILTER
)] = TCA_MAX
,
539 [RTM_FAM(RTM_NEWACTION
)] = TCAA_MAX
,
542 void __rta_fill(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
545 int size
= RTA_LENGTH(attrlen
);
547 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
548 rta
->rta_type
= attrtype
;
550 memcpy(RTA_DATA(rta
), data
, attrlen
);
551 memset(RTA_DATA(rta
) + attrlen
, 0, RTA_ALIGN(size
) - size
);
553 EXPORT_SYMBOL(__rta_fill
);
555 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned group
, int echo
)
557 struct sock
*rtnl
= net
->rtnl
;
560 NETLINK_CB(skb
).dst_group
= group
;
562 atomic_inc(&skb
->users
);
563 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
565 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
569 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
571 struct sock
*rtnl
= net
->rtnl
;
573 return nlmsg_unicast(rtnl
, skb
, pid
);
575 EXPORT_SYMBOL(rtnl_unicast
);
577 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
578 struct nlmsghdr
*nlh
, gfp_t flags
)
580 struct sock
*rtnl
= net
->rtnl
;
584 report
= nlmsg_report(nlh
);
586 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
588 EXPORT_SYMBOL(rtnl_notify
);
590 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
592 struct sock
*rtnl
= net
->rtnl
;
594 netlink_set_err(rtnl
, 0, group
, error
);
596 EXPORT_SYMBOL(rtnl_set_sk_err
);
598 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
603 mx
= nla_nest_start(skb
, RTA_METRICS
);
607 for (i
= 0; i
< RTAX_MAX
; i
++) {
610 if (nla_put_u32(skb
, i
+1, metrics
[i
]))
611 goto nla_put_failure
;
616 nla_nest_cancel(skb
, mx
);
620 return nla_nest_end(skb
, mx
);
623 nla_nest_cancel(skb
, mx
);
626 EXPORT_SYMBOL(rtnetlink_put_metrics
);
628 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
629 u32 ts
, u32 tsage
, long expires
, u32 error
)
631 struct rta_cacheinfo ci
= {
632 .rta_lastuse
= jiffies_to_clock_t(jiffies
- dst
->lastuse
),
633 .rta_used
= dst
->__use
,
634 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
642 ci
.rta_expires
= jiffies_to_clock_t(expires
);
644 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
646 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
648 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
650 unsigned char operstate
= dev
->operstate
;
652 switch (transition
) {
654 if ((operstate
== IF_OPER_DORMANT
||
655 operstate
== IF_OPER_UNKNOWN
) &&
657 operstate
= IF_OPER_UP
;
660 case IF_OPER_DORMANT
:
661 if (operstate
== IF_OPER_UP
||
662 operstate
== IF_OPER_UNKNOWN
)
663 operstate
= IF_OPER_DORMANT
;
667 if (dev
->operstate
!= operstate
) {
668 write_lock_bh(&dev_base_lock
);
669 dev
->operstate
= operstate
;
670 write_unlock_bh(&dev_base_lock
);
671 netdev_state_change(dev
);
675 static unsigned int rtnl_dev_combine_flags(const struct net_device
*dev
,
676 const struct ifinfomsg
*ifm
)
678 unsigned int flags
= ifm
->ifi_flags
;
680 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
682 flags
= (flags
& ifm
->ifi_change
) |
683 (dev
->flags
& ~ifm
->ifi_change
);
688 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
689 const struct rtnl_link_stats64
*b
)
691 a
->rx_packets
= b
->rx_packets
;
692 a
->tx_packets
= b
->tx_packets
;
693 a
->rx_bytes
= b
->rx_bytes
;
694 a
->tx_bytes
= b
->tx_bytes
;
695 a
->rx_errors
= b
->rx_errors
;
696 a
->tx_errors
= b
->tx_errors
;
697 a
->rx_dropped
= b
->rx_dropped
;
698 a
->tx_dropped
= b
->tx_dropped
;
700 a
->multicast
= b
->multicast
;
701 a
->collisions
= b
->collisions
;
703 a
->rx_length_errors
= b
->rx_length_errors
;
704 a
->rx_over_errors
= b
->rx_over_errors
;
705 a
->rx_crc_errors
= b
->rx_crc_errors
;
706 a
->rx_frame_errors
= b
->rx_frame_errors
;
707 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
708 a
->rx_missed_errors
= b
->rx_missed_errors
;
710 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
711 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
712 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
713 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
714 a
->tx_window_errors
= b
->tx_window_errors
;
716 a
->rx_compressed
= b
->rx_compressed
;
717 a
->tx_compressed
= b
->tx_compressed
;
720 static void copy_rtnl_link_stats64(void *v
, const struct rtnl_link_stats64
*b
)
722 memcpy(v
, b
, sizeof(*b
));
726 static inline int rtnl_vfinfo_size(const struct net_device
*dev
,
729 if (dev
->dev
.parent
&& dev_is_pci(dev
->dev
.parent
) &&
730 (ext_filter_mask
& RTEXT_FILTER_VF
)) {
731 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
732 size_t size
= nla_total_size(sizeof(struct nlattr
));
733 size
+= nla_total_size(num_vfs
* sizeof(struct nlattr
));
735 (nla_total_size(sizeof(struct ifla_vf_mac
)) +
736 nla_total_size(sizeof(struct ifla_vf_vlan
)) +
737 nla_total_size(sizeof(struct ifla_vf_tx_rate
)) +
738 nla_total_size(sizeof(struct ifla_vf_spoofchk
)));
744 static size_t rtnl_port_size(const struct net_device
*dev
)
746 size_t port_size
= nla_total_size(4) /* PORT_VF */
747 + nla_total_size(PORT_PROFILE_MAX
) /* PORT_PROFILE */
748 + nla_total_size(sizeof(struct ifla_port_vsi
))
750 + nla_total_size(PORT_UUID_MAX
) /* PORT_INSTANCE_UUID */
751 + nla_total_size(PORT_UUID_MAX
) /* PORT_HOST_UUID */
752 + nla_total_size(1) /* PROT_VDP_REQUEST */
753 + nla_total_size(2); /* PORT_VDP_RESPONSE */
754 size_t vf_ports_size
= nla_total_size(sizeof(struct nlattr
));
755 size_t vf_port_size
= nla_total_size(sizeof(struct nlattr
))
757 size_t port_self_size
= nla_total_size(sizeof(struct nlattr
))
760 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
)
762 if (dev_num_vf(dev
->dev
.parent
))
763 return port_self_size
+ vf_ports_size
+
764 vf_port_size
* dev_num_vf(dev
->dev
.parent
);
766 return port_self_size
;
769 static noinline
size_t if_nlmsg_size(const struct net_device
*dev
,
772 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
773 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
774 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
775 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
776 + nla_total_size(sizeof(struct rtnl_link_ifmap
))
777 + nla_total_size(sizeof(struct rtnl_link_stats
))
778 + nla_total_size(sizeof(struct rtnl_link_stats64
))
779 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
780 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
781 + nla_total_size(4) /* IFLA_TXQLEN */
782 + nla_total_size(4) /* IFLA_WEIGHT */
783 + nla_total_size(4) /* IFLA_MTU */
784 + nla_total_size(4) /* IFLA_LINK */
785 + nla_total_size(4) /* IFLA_MASTER */
786 + nla_total_size(4) /* IFLA_PROMISCUITY */
787 + nla_total_size(1) /* IFLA_OPERSTATE */
788 + nla_total_size(1) /* IFLA_LINKMODE */
789 + nla_total_size(ext_filter_mask
790 & RTEXT_FILTER_VF
? 4 : 0) /* IFLA_NUM_VF */
791 + rtnl_vfinfo_size(dev
, ext_filter_mask
) /* IFLA_VFINFO_LIST */
792 + rtnl_port_size(dev
) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
793 + rtnl_link_get_size(dev
) /* IFLA_LINKINFO */
794 + rtnl_link_get_af_size(dev
); /* IFLA_AF_SPEC */
797 static int rtnl_vf_ports_fill(struct sk_buff
*skb
, struct net_device
*dev
)
799 struct nlattr
*vf_ports
;
800 struct nlattr
*vf_port
;
804 vf_ports
= nla_nest_start(skb
, IFLA_VF_PORTS
);
808 for (vf
= 0; vf
< dev_num_vf(dev
->dev
.parent
); vf
++) {
809 vf_port
= nla_nest_start(skb
, IFLA_VF_PORT
);
811 goto nla_put_failure
;
812 if (nla_put_u32(skb
, IFLA_PORT_VF
, vf
))
813 goto nla_put_failure
;
814 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, vf
, skb
);
815 if (err
== -EMSGSIZE
)
816 goto nla_put_failure
;
818 nla_nest_cancel(skb
, vf_port
);
821 nla_nest_end(skb
, vf_port
);
824 nla_nest_end(skb
, vf_ports
);
829 nla_nest_cancel(skb
, vf_ports
);
833 static int rtnl_port_self_fill(struct sk_buff
*skb
, struct net_device
*dev
)
835 struct nlattr
*port_self
;
838 port_self
= nla_nest_start(skb
, IFLA_PORT_SELF
);
842 err
= dev
->netdev_ops
->ndo_get_vf_port(dev
, PORT_SELF_VF
, skb
);
844 nla_nest_cancel(skb
, port_self
);
845 return (err
== -EMSGSIZE
) ? err
: 0;
848 nla_nest_end(skb
, port_self
);
853 static int rtnl_port_fill(struct sk_buff
*skb
, struct net_device
*dev
)
857 if (!dev
->netdev_ops
->ndo_get_vf_port
|| !dev
->dev
.parent
)
860 err
= rtnl_port_self_fill(skb
, dev
);
864 if (dev_num_vf(dev
->dev
.parent
)) {
865 err
= rtnl_vf_ports_fill(skb
, dev
);
873 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
874 int type
, u32 pid
, u32 seq
, u32 change
,
875 unsigned int flags
, u32 ext_filter_mask
)
877 struct ifinfomsg
*ifm
;
878 struct nlmsghdr
*nlh
;
879 struct rtnl_link_stats64 temp
;
880 const struct rtnl_link_stats64
*stats
;
881 struct nlattr
*attr
, *af_spec
;
882 struct rtnl_af_ops
*af_ops
;
885 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
889 ifm
= nlmsg_data(nlh
);
890 ifm
->ifi_family
= AF_UNSPEC
;
892 ifm
->ifi_type
= dev
->type
;
893 ifm
->ifi_index
= dev
->ifindex
;
894 ifm
->ifi_flags
= dev_get_flags(dev
);
895 ifm
->ifi_change
= change
;
897 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
898 nla_put_u32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
) ||
899 nla_put_u8(skb
, IFLA_OPERSTATE
,
900 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
) ||
901 nla_put_u8(skb
, IFLA_LINKMODE
, dev
->link_mode
) ||
902 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
903 nla_put_u32(skb
, IFLA_GROUP
, dev
->group
) ||
904 nla_put_u32(skb
, IFLA_PROMISCUITY
, dev
->promiscuity
) ||
905 (dev
->ifindex
!= dev
->iflink
&&
906 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)) ||
908 nla_put_u32(skb
, IFLA_MASTER
, dev
->master
->ifindex
)) ||
910 nla_put_string(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
)) ||
912 nla_put_string(skb
, IFLA_IFALIAS
, dev
->ifalias
)))
913 goto nla_put_failure
;
916 struct rtnl_link_ifmap map
= {
917 .mem_start
= dev
->mem_start
,
918 .mem_end
= dev
->mem_end
,
919 .base_addr
= dev
->base_addr
,
922 .port
= dev
->if_port
,
924 if (nla_put(skb
, IFLA_MAP
, sizeof(map
), &map
))
925 goto nla_put_failure
;
929 if (nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
) ||
930 nla_put(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
))
931 goto nla_put_failure
;
934 attr
= nla_reserve(skb
, IFLA_STATS
,
935 sizeof(struct rtnl_link_stats
));
937 goto nla_put_failure
;
939 stats
= dev_get_stats(dev
, &temp
);
940 copy_rtnl_link_stats(nla_data(attr
), stats
);
942 attr
= nla_reserve(skb
, IFLA_STATS64
,
943 sizeof(struct rtnl_link_stats64
));
945 goto nla_put_failure
;
946 copy_rtnl_link_stats64(nla_data(attr
), stats
);
948 if (dev
->dev
.parent
&& (ext_filter_mask
& RTEXT_FILTER_VF
) &&
949 nla_put_u32(skb
, IFLA_NUM_VF
, dev_num_vf(dev
->dev
.parent
)))
950 goto nla_put_failure
;
952 if (dev
->netdev_ops
->ndo_get_vf_config
&& dev
->dev
.parent
953 && (ext_filter_mask
& RTEXT_FILTER_VF
)) {
956 struct nlattr
*vfinfo
, *vf
;
957 int num_vfs
= dev_num_vf(dev
->dev
.parent
);
959 vfinfo
= nla_nest_start(skb
, IFLA_VFINFO_LIST
);
961 goto nla_put_failure
;
962 for (i
= 0; i
< num_vfs
; i
++) {
963 struct ifla_vf_info ivi
;
964 struct ifla_vf_mac vf_mac
;
965 struct ifla_vf_vlan vf_vlan
;
966 struct ifla_vf_tx_rate vf_tx_rate
;
967 struct ifla_vf_spoofchk vf_spoofchk
;
970 * Not all SR-IOV capable drivers support the
971 * spoofcheck query. Preset to -1 so the user
972 * space tool can detect that the driver didn't
976 if (dev
->netdev_ops
->ndo_get_vf_config(dev
, i
, &ivi
))
981 vf_spoofchk
.vf
= ivi
.vf
;
983 memcpy(vf_mac
.mac
, ivi
.mac
, sizeof(ivi
.mac
));
984 vf_vlan
.vlan
= ivi
.vlan
;
985 vf_vlan
.qos
= ivi
.qos
;
986 vf_tx_rate
.rate
= ivi
.tx_rate
;
987 vf_spoofchk
.setting
= ivi
.spoofchk
;
988 vf
= nla_nest_start(skb
, IFLA_VF_INFO
);
990 nla_nest_cancel(skb
, vfinfo
);
991 goto nla_put_failure
;
993 if (nla_put(skb
, IFLA_VF_MAC
, sizeof(vf_mac
), &vf_mac
) ||
994 nla_put(skb
, IFLA_VF_VLAN
, sizeof(vf_vlan
), &vf_vlan
) ||
995 nla_put(skb
, IFLA_VF_TX_RATE
, sizeof(vf_tx_rate
),
997 nla_put(skb
, IFLA_VF_SPOOFCHK
, sizeof(vf_spoofchk
),
999 goto nla_put_failure
;
1000 nla_nest_end(skb
, vf
);
1002 nla_nest_end(skb
, vfinfo
);
1005 if (rtnl_port_fill(skb
, dev
))
1006 goto nla_put_failure
;
1008 if (dev
->rtnl_link_ops
) {
1009 if (rtnl_link_fill(skb
, dev
) < 0)
1010 goto nla_put_failure
;
1013 if (!(af_spec
= nla_nest_start(skb
, IFLA_AF_SPEC
)))
1014 goto nla_put_failure
;
1016 list_for_each_entry(af_ops
, &rtnl_af_ops
, list
) {
1017 if (af_ops
->fill_link_af
) {
1021 if (!(af
= nla_nest_start(skb
, af_ops
->family
)))
1022 goto nla_put_failure
;
1024 err
= af_ops
->fill_link_af(skb
, dev
);
1027 * Caller may return ENODATA to indicate that there
1028 * was no data to be dumped. This is not an error, it
1029 * means we should trim the attribute header and
1032 if (err
== -ENODATA
)
1033 nla_nest_cancel(skb
, af
);
1035 goto nla_put_failure
;
1037 nla_nest_end(skb
, af
);
1041 nla_nest_end(skb
, af_spec
);
1043 return nlmsg_end(skb
, nlh
);
1046 nlmsg_cancel(skb
, nlh
);
1050 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1052 struct net
*net
= sock_net(skb
->sk
);
1055 struct net_device
*dev
;
1056 struct hlist_head
*head
;
1057 struct hlist_node
*node
;
1058 struct nlattr
*tb
[IFLA_MAX
+1];
1059 u32 ext_filter_mask
= 0;
1062 s_idx
= cb
->args
[1];
1065 cb
->seq
= net
->dev_base_seq
;
1067 if (nlmsg_parse(cb
->nlh
, sizeof(struct rtgenmsg
), tb
, IFLA_MAX
,
1068 ifla_policy
) >= 0) {
1070 if (tb
[IFLA_EXT_MASK
])
1071 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1074 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
1076 head
= &net
->dev_index_head
[h
];
1077 hlist_for_each_entry_rcu(dev
, node
, head
, index_hlist
) {
1080 if (rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
1081 NETLINK_CB(cb
->skb
).pid
,
1082 cb
->nlh
->nlmsg_seq
, 0,
1084 ext_filter_mask
) <= 0)
1087 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1100 const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
1101 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
1102 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1103 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
1104 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
1105 [IFLA_MTU
] = { .type
= NLA_U32
},
1106 [IFLA_LINK
] = { .type
= NLA_U32
},
1107 [IFLA_MASTER
] = { .type
= NLA_U32
},
1108 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
1109 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
1110 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
1111 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
1112 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
1113 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
1114 [IFLA_NET_NS_FD
] = { .type
= NLA_U32
},
1115 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
1116 [IFLA_VFINFO_LIST
] = {. type
= NLA_NESTED
},
1117 [IFLA_VF_PORTS
] = { .type
= NLA_NESTED
},
1118 [IFLA_PORT_SELF
] = { .type
= NLA_NESTED
},
1119 [IFLA_AF_SPEC
] = { .type
= NLA_NESTED
},
1120 [IFLA_EXT_MASK
] = { .type
= NLA_U32
},
1121 [IFLA_PROMISCUITY
] = { .type
= NLA_U32
},
1123 EXPORT_SYMBOL(ifla_policy
);
1125 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
1126 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
1127 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
1130 static const struct nla_policy ifla_vfinfo_policy
[IFLA_VF_INFO_MAX
+1] = {
1131 [IFLA_VF_INFO
] = { .type
= NLA_NESTED
},
1134 static const struct nla_policy ifla_vf_policy
[IFLA_VF_MAX
+1] = {
1135 [IFLA_VF_MAC
] = { .type
= NLA_BINARY
,
1136 .len
= sizeof(struct ifla_vf_mac
) },
1137 [IFLA_VF_VLAN
] = { .type
= NLA_BINARY
,
1138 .len
= sizeof(struct ifla_vf_vlan
) },
1139 [IFLA_VF_TX_RATE
] = { .type
= NLA_BINARY
,
1140 .len
= sizeof(struct ifla_vf_tx_rate
) },
1141 [IFLA_VF_SPOOFCHK
] = { .type
= NLA_BINARY
,
1142 .len
= sizeof(struct ifla_vf_spoofchk
) },
1145 static const struct nla_policy ifla_port_policy
[IFLA_PORT_MAX
+1] = {
1146 [IFLA_PORT_VF
] = { .type
= NLA_U32
},
1147 [IFLA_PORT_PROFILE
] = { .type
= NLA_STRING
,
1148 .len
= PORT_PROFILE_MAX
},
1149 [IFLA_PORT_VSI_TYPE
] = { .type
= NLA_BINARY
,
1150 .len
= sizeof(struct ifla_port_vsi
)},
1151 [IFLA_PORT_INSTANCE_UUID
] = { .type
= NLA_BINARY
,
1152 .len
= PORT_UUID_MAX
},
1153 [IFLA_PORT_HOST_UUID
] = { .type
= NLA_STRING
,
1154 .len
= PORT_UUID_MAX
},
1155 [IFLA_PORT_REQUEST
] = { .type
= NLA_U8
, },
1156 [IFLA_PORT_RESPONSE
] = { .type
= NLA_U16
, },
1159 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
1162 /* Examine the link attributes and figure out which
1163 * network namespace we are talking about.
1165 if (tb
[IFLA_NET_NS_PID
])
1166 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
1167 else if (tb
[IFLA_NET_NS_FD
])
1168 net
= get_net_ns_by_fd(nla_get_u32(tb
[IFLA_NET_NS_FD
]));
1170 net
= get_net(src_net
);
1173 EXPORT_SYMBOL(rtnl_link_get_net
);
1175 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
1178 if (tb
[IFLA_ADDRESS
] &&
1179 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
1182 if (tb
[IFLA_BROADCAST
] &&
1183 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
1187 if (tb
[IFLA_AF_SPEC
]) {
1191 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1192 const struct rtnl_af_ops
*af_ops
;
1194 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1195 return -EAFNOSUPPORT
;
1197 if (!af_ops
->set_link_af
)
1200 if (af_ops
->validate_link_af
) {
1201 err
= af_ops
->validate_link_af(dev
, af
);
1211 static int do_setvfinfo(struct net_device
*dev
, struct nlattr
*attr
)
1213 int rem
, err
= -EINVAL
;
1215 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1217 nla_for_each_nested(vf
, attr
, rem
) {
1218 switch (nla_type(vf
)) {
1220 struct ifla_vf_mac
*ivm
;
1223 if (ops
->ndo_set_vf_mac
)
1224 err
= ops
->ndo_set_vf_mac(dev
, ivm
->vf
,
1228 case IFLA_VF_VLAN
: {
1229 struct ifla_vf_vlan
*ivv
;
1232 if (ops
->ndo_set_vf_vlan
)
1233 err
= ops
->ndo_set_vf_vlan(dev
, ivv
->vf
,
1238 case IFLA_VF_TX_RATE
: {
1239 struct ifla_vf_tx_rate
*ivt
;
1242 if (ops
->ndo_set_vf_tx_rate
)
1243 err
= ops
->ndo_set_vf_tx_rate(dev
, ivt
->vf
,
1247 case IFLA_VF_SPOOFCHK
: {
1248 struct ifla_vf_spoofchk
*ivs
;
1251 if (ops
->ndo_set_vf_spoofchk
)
1252 err
= ops
->ndo_set_vf_spoofchk(dev
, ivs
->vf
,
1266 static int do_set_master(struct net_device
*dev
, int ifindex
)
1268 struct net_device
*master_dev
;
1269 const struct net_device_ops
*ops
;
1273 if (dev
->master
->ifindex
== ifindex
)
1275 ops
= dev
->master
->netdev_ops
;
1276 if (ops
->ndo_del_slave
) {
1277 err
= ops
->ndo_del_slave(dev
->master
, dev
);
1286 master_dev
= __dev_get_by_index(dev_net(dev
), ifindex
);
1289 ops
= master_dev
->netdev_ops
;
1290 if (ops
->ndo_add_slave
) {
1291 err
= ops
->ndo_add_slave(master_dev
, dev
);
1301 static int do_setlink(struct net_device
*dev
, struct ifinfomsg
*ifm
,
1302 struct nlattr
**tb
, char *ifname
, int modified
)
1304 const struct net_device_ops
*ops
= dev
->netdev_ops
;
1305 int send_addr_notify
= 0;
1308 if (tb
[IFLA_NET_NS_PID
] || tb
[IFLA_NET_NS_FD
]) {
1309 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
1314 err
= dev_change_net_namespace(dev
, net
, ifname
);
1322 struct rtnl_link_ifmap
*u_map
;
1325 if (!ops
->ndo_set_config
) {
1330 if (!netif_device_present(dev
)) {
1335 u_map
= nla_data(tb
[IFLA_MAP
]);
1336 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
1337 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
1338 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
1339 k_map
.irq
= (unsigned char) u_map
->irq
;
1340 k_map
.dma
= (unsigned char) u_map
->dma
;
1341 k_map
.port
= (unsigned char) u_map
->port
;
1343 err
= ops
->ndo_set_config(dev
, &k_map
);
1350 if (tb
[IFLA_ADDRESS
]) {
1351 struct sockaddr
*sa
;
1354 if (!ops
->ndo_set_mac_address
) {
1359 if (!netif_device_present(dev
)) {
1364 len
= sizeof(sa_family_t
) + dev
->addr_len
;
1365 sa
= kmalloc(len
, GFP_KERNEL
);
1370 sa
->sa_family
= dev
->type
;
1371 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
1373 err
= ops
->ndo_set_mac_address(dev
, sa
);
1377 send_addr_notify
= 1;
1382 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
1388 if (tb
[IFLA_GROUP
]) {
1389 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1394 * Interface selected by interface index but interface
1395 * name provided implies that a name change has been
1398 if (ifm
->ifi_index
> 0 && ifname
[0]) {
1399 err
= dev_change_name(dev
, ifname
);
1405 if (tb
[IFLA_IFALIAS
]) {
1406 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
1407 nla_len(tb
[IFLA_IFALIAS
]));
1413 if (tb
[IFLA_BROADCAST
]) {
1414 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
1415 send_addr_notify
= 1;
1418 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
1419 err
= dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1424 if (tb
[IFLA_MASTER
]) {
1425 err
= do_set_master(dev
, nla_get_u32(tb
[IFLA_MASTER
]));
1431 if (tb
[IFLA_TXQLEN
])
1432 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1434 if (tb
[IFLA_OPERSTATE
])
1435 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1437 if (tb
[IFLA_LINKMODE
]) {
1438 write_lock_bh(&dev_base_lock
);
1439 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1440 write_unlock_bh(&dev_base_lock
);
1443 if (tb
[IFLA_VFINFO_LIST
]) {
1444 struct nlattr
*attr
;
1446 nla_for_each_nested(attr
, tb
[IFLA_VFINFO_LIST
], rem
) {
1447 if (nla_type(attr
) != IFLA_VF_INFO
) {
1451 err
= do_setvfinfo(dev
, attr
);
1459 if (tb
[IFLA_VF_PORTS
]) {
1460 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1461 struct nlattr
*attr
;
1466 if (!ops
->ndo_set_vf_port
)
1469 nla_for_each_nested(attr
, tb
[IFLA_VF_PORTS
], rem
) {
1470 if (nla_type(attr
) != IFLA_VF_PORT
)
1472 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
1473 attr
, ifla_port_policy
);
1476 if (!port
[IFLA_PORT_VF
]) {
1480 vf
= nla_get_u32(port
[IFLA_PORT_VF
]);
1481 err
= ops
->ndo_set_vf_port(dev
, vf
, port
);
1489 if (tb
[IFLA_PORT_SELF
]) {
1490 struct nlattr
*port
[IFLA_PORT_MAX
+1];
1492 err
= nla_parse_nested(port
, IFLA_PORT_MAX
,
1493 tb
[IFLA_PORT_SELF
], ifla_port_policy
);
1498 if (ops
->ndo_set_vf_port
)
1499 err
= ops
->ndo_set_vf_port(dev
, PORT_SELF_VF
, port
);
1505 if (tb
[IFLA_AF_SPEC
]) {
1509 nla_for_each_nested(af
, tb
[IFLA_AF_SPEC
], rem
) {
1510 const struct rtnl_af_ops
*af_ops
;
1512 if (!(af_ops
= rtnl_af_lookup(nla_type(af
))))
1515 err
= af_ops
->set_link_af(dev
, af
);
1525 if (err
< 0 && modified
&& net_ratelimit())
1526 printk(KERN_WARNING
"A link change request failed with "
1527 "some changes committed already. Interface %s may "
1528 "have been left with an inconsistent configuration, "
1529 "please check.\n", dev
->name
);
1531 if (send_addr_notify
)
1532 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
1537 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
1539 struct net
*net
= sock_net(skb
->sk
);
1540 struct ifinfomsg
*ifm
;
1541 struct net_device
*dev
;
1543 struct nlattr
*tb
[IFLA_MAX
+1];
1544 char ifname
[IFNAMSIZ
];
1546 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1550 if (tb
[IFLA_IFNAME
])
1551 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1556 ifm
= nlmsg_data(nlh
);
1557 if (ifm
->ifi_index
> 0)
1558 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1559 else if (tb
[IFLA_IFNAME
])
1560 dev
= __dev_get_by_name(net
, ifname
);
1569 err
= validate_linkmsg(dev
, tb
);
1573 err
= do_setlink(dev
, ifm
, tb
, ifname
, 0);
1578 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
1580 struct net
*net
= sock_net(skb
->sk
);
1581 const struct rtnl_link_ops
*ops
;
1582 struct net_device
*dev
;
1583 struct ifinfomsg
*ifm
;
1584 char ifname
[IFNAMSIZ
];
1585 struct nlattr
*tb
[IFLA_MAX
+1];
1587 LIST_HEAD(list_kill
);
1589 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1593 if (tb
[IFLA_IFNAME
])
1594 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1596 ifm
= nlmsg_data(nlh
);
1597 if (ifm
->ifi_index
> 0)
1598 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1599 else if (tb
[IFLA_IFNAME
])
1600 dev
= __dev_get_by_name(net
, ifname
);
1607 ops
= dev
->rtnl_link_ops
;
1611 ops
->dellink(dev
, &list_kill
);
1612 unregister_netdevice_many(&list_kill
);
1613 list_del(&list_kill
);
1617 int rtnl_configure_link(struct net_device
*dev
, const struct ifinfomsg
*ifm
)
1619 unsigned int old_flags
;
1622 old_flags
= dev
->flags
;
1623 if (ifm
&& (ifm
->ifi_flags
|| ifm
->ifi_change
)) {
1624 err
= __dev_change_flags(dev
, rtnl_dev_combine_flags(dev
, ifm
));
1629 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZED
;
1630 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U);
1632 __dev_notify_flags(dev
, old_flags
);
1635 EXPORT_SYMBOL(rtnl_configure_link
);
1637 struct net_device
*rtnl_create_link(struct net
*src_net
, struct net
*net
,
1638 char *ifname
, const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
1641 struct net_device
*dev
;
1642 unsigned int num_queues
= 1;
1644 if (ops
->get_tx_queues
) {
1645 err
= ops
->get_tx_queues(src_net
, tb
);
1652 dev
= alloc_netdev_mq(ops
->priv_size
, ifname
, ops
->setup
, num_queues
);
1656 dev_net_set(dev
, net
);
1657 dev
->rtnl_link_ops
= ops
;
1658 dev
->rtnl_link_state
= RTNL_LINK_INITIALIZING
;
1661 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
1662 if (tb
[IFLA_ADDRESS
])
1663 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
1664 nla_len(tb
[IFLA_ADDRESS
]));
1665 if (tb
[IFLA_BROADCAST
])
1666 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
1667 nla_len(tb
[IFLA_BROADCAST
]));
1668 if (tb
[IFLA_TXQLEN
])
1669 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1670 if (tb
[IFLA_OPERSTATE
])
1671 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1672 if (tb
[IFLA_LINKMODE
])
1673 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1675 dev_set_group(dev
, nla_get_u32(tb
[IFLA_GROUP
]));
1680 return ERR_PTR(err
);
1682 EXPORT_SYMBOL(rtnl_create_link
);
1684 static int rtnl_group_changelink(struct net
*net
, int group
,
1685 struct ifinfomsg
*ifm
,
1688 struct net_device
*dev
;
1691 for_each_netdev(net
, dev
) {
1692 if (dev
->group
== group
) {
1693 err
= do_setlink(dev
, ifm
, tb
, NULL
, 0);
1702 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
1704 struct net
*net
= sock_net(skb
->sk
);
1705 const struct rtnl_link_ops
*ops
;
1706 struct net_device
*dev
;
1707 struct ifinfomsg
*ifm
;
1708 char kind
[MODULE_NAME_LEN
];
1709 char ifname
[IFNAMSIZ
];
1710 struct nlattr
*tb
[IFLA_MAX
+1];
1711 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
1714 #ifdef CONFIG_MODULES
1717 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1721 if (tb
[IFLA_IFNAME
])
1722 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1726 ifm
= nlmsg_data(nlh
);
1727 if (ifm
->ifi_index
> 0)
1728 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1731 dev
= __dev_get_by_name(net
, ifname
);
1736 err
= validate_linkmsg(dev
, tb
);
1740 if (tb
[IFLA_LINKINFO
]) {
1741 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
1742 tb
[IFLA_LINKINFO
], ifla_info_policy
);
1746 memset(linkinfo
, 0, sizeof(linkinfo
));
1748 if (linkinfo
[IFLA_INFO_KIND
]) {
1749 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
1750 ops
= rtnl_link_ops_get(kind
);
1757 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 0], **data
= NULL
;
1758 struct net
*dest_net
;
1761 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
1762 err
= nla_parse_nested(attr
, ops
->maxtype
,
1763 linkinfo
[IFLA_INFO_DATA
],
1769 if (ops
->validate
) {
1770 err
= ops
->validate(tb
, data
);
1779 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1781 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1784 if (linkinfo
[IFLA_INFO_DATA
]) {
1785 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
1789 err
= ops
->changelink(dev
, tb
, data
);
1795 return do_setlink(dev
, ifm
, tb
, ifname
, modified
);
1798 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
1799 if (ifm
->ifi_index
== 0 && tb
[IFLA_GROUP
])
1800 return rtnl_group_changelink(net
,
1801 nla_get_u32(tb
[IFLA_GROUP
]),
1808 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
1812 #ifdef CONFIG_MODULES
1815 request_module("rtnl-link-%s", kind
);
1817 ops
= rtnl_link_ops_get(kind
);
1826 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
1828 dest_net
= rtnl_link_get_net(net
, tb
);
1829 if (IS_ERR(dest_net
))
1830 return PTR_ERR(dest_net
);
1832 dev
= rtnl_create_link(net
, dest_net
, ifname
, ops
, tb
);
1836 else if (ops
->newlink
)
1837 err
= ops
->newlink(net
, dev
, tb
, data
);
1839 err
= register_netdevice(dev
);
1841 if (err
< 0 && !IS_ERR(dev
))
1846 err
= rtnl_configure_link(dev
, ifm
);
1848 unregister_netdevice(dev
);
1855 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
1857 struct net
*net
= sock_net(skb
->sk
);
1858 struct ifinfomsg
*ifm
;
1859 char ifname
[IFNAMSIZ
];
1860 struct nlattr
*tb
[IFLA_MAX
+1];
1861 struct net_device
*dev
= NULL
;
1862 struct sk_buff
*nskb
;
1864 u32 ext_filter_mask
= 0;
1866 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1870 if (tb
[IFLA_IFNAME
])
1871 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1873 if (tb
[IFLA_EXT_MASK
])
1874 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1876 ifm
= nlmsg_data(nlh
);
1877 if (ifm
->ifi_index
> 0)
1878 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1879 else if (tb
[IFLA_IFNAME
])
1880 dev
= __dev_get_by_name(net
, ifname
);
1887 nskb
= nlmsg_new(if_nlmsg_size(dev
, ext_filter_mask
), GFP_KERNEL
);
1891 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).pid
,
1892 nlh
->nlmsg_seq
, 0, 0, ext_filter_mask
);
1894 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1895 WARN_ON(err
== -EMSGSIZE
);
1898 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).pid
);
1903 static u16
rtnl_calcit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1905 struct net
*net
= sock_net(skb
->sk
);
1906 struct net_device
*dev
;
1907 struct nlattr
*tb
[IFLA_MAX
+1];
1908 u32 ext_filter_mask
= 0;
1909 u16 min_ifinfo_dump_size
= 0;
1911 if (nlmsg_parse(nlh
, sizeof(struct rtgenmsg
), tb
, IFLA_MAX
,
1912 ifla_policy
) >= 0) {
1913 if (tb
[IFLA_EXT_MASK
])
1914 ext_filter_mask
= nla_get_u32(tb
[IFLA_EXT_MASK
]);
1917 if (!ext_filter_mask
)
1918 return NLMSG_GOODSIZE
;
1920 * traverse the list of net devices and compute the minimum
1921 * buffer size based upon the filter mask.
1923 list_for_each_entry(dev
, &net
->dev_base_head
, dev_list
) {
1924 min_ifinfo_dump_size
= max_t(u16
, min_ifinfo_dump_size
,
1929 return min_ifinfo_dump_size
;
1932 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1935 int s_idx
= cb
->family
;
1939 for (idx
= 1; idx
<= RTNL_FAMILY_MAX
; idx
++) {
1940 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
1941 if (idx
< s_idx
|| idx
== PF_PACKET
)
1943 if (rtnl_msg_handlers
[idx
] == NULL
||
1944 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
1947 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
1948 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
1956 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned change
)
1958 struct net
*net
= dev_net(dev
);
1959 struct sk_buff
*skb
;
1961 size_t if_info_size
;
1963 skb
= nlmsg_new((if_info_size
= if_nlmsg_size(dev
, 0)), GFP_KERNEL
);
1967 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0, 0);
1969 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1970 WARN_ON(err
== -EMSGSIZE
);
1974 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_KERNEL
);
1978 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
1981 /* Protected by RTNL sempahore. */
1982 static struct rtattr
**rta_buf
;
1983 static int rtattr_max
;
1985 /* Process one rtnetlink message. */
1987 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1989 struct net
*net
= sock_net(skb
->sk
);
1990 rtnl_doit_func doit
;
1997 type
= nlh
->nlmsg_type
;
2003 /* All the messages must have at least 1 byte length */
2004 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
2007 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
2011 if (kind
!= 2 && !capable(CAP_NET_ADMIN
))
2014 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
2016 rtnl_dumpit_func dumpit
;
2017 rtnl_calcit_func calcit
;
2018 u16 min_dump_alloc
= 0;
2020 dumpit
= rtnl_get_dumpit(family
, type
);
2023 calcit
= rtnl_get_calcit(family
, type
);
2025 min_dump_alloc
= calcit(skb
, nlh
);
2030 struct netlink_dump_control c
= {
2032 .min_dump_alloc
= min_dump_alloc
,
2034 err
= netlink_dump_start(rtnl
, skb
, nlh
, &c
);
2040 memset(rta_buf
, 0, (rtattr_max
* sizeof(struct rtattr
*)));
2042 min_len
= rtm_min
[sz_idx
];
2043 if (nlh
->nlmsg_len
< min_len
)
2046 if (nlh
->nlmsg_len
> min_len
) {
2047 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
2048 struct rtattr
*attr
= (void *)nlh
+ NLMSG_ALIGN(min_len
);
2050 while (RTA_OK(attr
, attrlen
)) {
2051 unsigned flavor
= attr
->rta_type
;
2053 if (flavor
> rta_max
[sz_idx
])
2055 rta_buf
[flavor
-1] = attr
;
2057 attr
= RTA_NEXT(attr
, attrlen
);
2061 doit
= rtnl_get_doit(family
, type
);
2065 return doit(skb
, nlh
, (void *)&rta_buf
[0]);
2068 static void rtnetlink_rcv(struct sk_buff
*skb
)
2071 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
2075 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2077 struct net_device
*dev
= ptr
;
2083 case NETDEV_POST_INIT
:
2084 case NETDEV_REGISTER
:
2086 case NETDEV_PRE_TYPE_CHANGE
:
2087 case NETDEV_GOING_DOWN
:
2088 case NETDEV_UNREGISTER
:
2089 case NETDEV_UNREGISTER_BATCH
:
2090 case NETDEV_RELEASE
:
2094 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
2100 static struct notifier_block rtnetlink_dev_notifier
= {
2101 .notifier_call
= rtnetlink_event
,
2105 static int __net_init
rtnetlink_net_init(struct net
*net
)
2108 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, RTNLGRP_MAX
,
2109 rtnetlink_rcv
, &rtnl_mutex
, THIS_MODULE
);
2116 static void __net_exit
rtnetlink_net_exit(struct net
*net
)
2118 netlink_kernel_release(net
->rtnl
);
2122 static struct pernet_operations rtnetlink_net_ops
= {
2123 .init
= rtnetlink_net_init
,
2124 .exit
= rtnetlink_net_exit
,
2127 void __init
rtnetlink_init(void)
2132 for (i
= 0; i
< ARRAY_SIZE(rta_max
); i
++)
2133 if (rta_max
[i
] > rtattr_max
)
2134 rtattr_max
= rta_max
[i
];
2135 rta_buf
= kmalloc(rtattr_max
* sizeof(struct rtattr
*), GFP_KERNEL
);
2137 panic("rtnetlink_init: cannot allocate rta_buf\n");
2139 if (register_pernet_subsys(&rtnetlink_net_ops
))
2140 panic("rtnetlink_init: cannot initialize rtnetlink\n");
2142 netlink_set_nonroot(NETLINK_ROUTE
, NL_NONROOT_RECV
);
2143 register_netdevice_notifier(&rtnetlink_dev_notifier
);
2145 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
,
2146 rtnl_dump_ifinfo
, rtnl_calcit
);
2147 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
, NULL
);
2148 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
, NULL
);
2149 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
, NULL
);
2151 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
, NULL
);
2152 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
, NULL
);