Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 37 | #include <linux/if_addr.h> |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 38 | #include <linux/if_bridge.h> |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 39 | #include <linux/if_vlan.h> |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 40 | #include <linux/pci.h> |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 41 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <linux/inet.h> |
| 46 | #include <linux/netdevice.h> |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 47 | #include <net/switchdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <net/ip.h> |
| 49 | #include <net/protocol.h> |
| 50 | #include <net/arp.h> |
| 51 | #include <net/route.h> |
| 52 | #include <net/udp.h> |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 53 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <net/sock.h> |
| 55 | #include <net/pkt_sched.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 56 | #include <net/fib_rules.h> |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 57 | #include <net/rtnetlink.h> |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 58 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 60 | struct rtnl_link { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 61 | rtnl_doit_func doit; |
| 62 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 63 | rtnl_calcit_func calcit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 66 | static DEFINE_MUTEX(rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | void rtnl_lock(void) |
| 69 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 70 | mutex_lock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 72 | EXPORT_SYMBOL(rtnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 74 | static struct sk_buff *defer_kfree_skb_list; |
| 75 | void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) |
| 76 | { |
| 77 | if (head && tail) { |
| 78 | tail->next = defer_kfree_skb_list; |
| 79 | defer_kfree_skb_list = head; |
| 80 | } |
| 81 | } |
| 82 | EXPORT_SYMBOL(rtnl_kfree_skbs); |
| 83 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 84 | void __rtnl_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 86 | struct sk_buff *head = defer_kfree_skb_list; |
| 87 | |
| 88 | defer_kfree_skb_list = NULL; |
| 89 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 90 | mutex_unlock(&rtnl_mutex); |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 91 | |
| 92 | while (head) { |
| 93 | struct sk_buff *next = head->next; |
| 94 | |
| 95 | kfree_skb(head); |
| 96 | cond_resched(); |
| 97 | head = next; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | void rtnl_unlock(void) |
| 102 | { |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 103 | /* This fellow will unlock it for us. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | netdev_run_todo(); |
| 105 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 106 | EXPORT_SYMBOL(rtnl_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 108 | int rtnl_trylock(void) |
| 109 | { |
| 110 | return mutex_trylock(&rtnl_mutex); |
| 111 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 112 | EXPORT_SYMBOL(rtnl_trylock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 113 | |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 114 | int rtnl_is_locked(void) |
| 115 | { |
| 116 | return mutex_is_locked(&rtnl_mutex); |
| 117 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 118 | EXPORT_SYMBOL(rtnl_is_locked); |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 119 | |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 120 | #ifdef CONFIG_PROVE_LOCKING |
Yaowei Bai | 0cbf334 | 2015-10-08 21:29:02 +0800 | [diff] [blame] | 121 | bool lockdep_rtnl_is_held(void) |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 122 | { |
| 123 | return lockdep_is_held(&rtnl_mutex); |
| 124 | } |
| 125 | EXPORT_SYMBOL(lockdep_rtnl_is_held); |
| 126 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ |
| 127 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 128 | static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 129 | |
| 130 | static inline int rtm_msgindex(int msgtype) |
| 131 | { |
| 132 | int msgindex = msgtype - RTM_BASE; |
| 133 | |
| 134 | /* |
| 135 | * msgindex < 0 implies someone tried to register a netlink |
| 136 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that |
| 137 | * the message type has not been added to linux/rtnetlink.h |
| 138 | */ |
| 139 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); |
| 140 | |
| 141 | return msgindex; |
| 142 | } |
| 143 | |
| 144 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) |
| 145 | { |
| 146 | struct rtnl_link *tab; |
| 147 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 148 | if (protocol <= RTNL_FAMILY_MAX) |
Patrick McHardy | 0f87b1d | 2010-04-13 05:03:17 +0000 | [diff] [blame] | 149 | tab = rtnl_msg_handlers[protocol]; |
| 150 | else |
| 151 | tab = NULL; |
| 152 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 153 | if (tab == NULL || tab[msgindex].doit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 154 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 155 | |
Hans Zhang | c80bbea | 2012-10-22 22:21:23 +0000 | [diff] [blame] | 156 | return tab[msgindex].doit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) |
| 160 | { |
| 161 | struct rtnl_link *tab; |
| 162 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 163 | if (protocol <= RTNL_FAMILY_MAX) |
Patrick McHardy | 0f87b1d | 2010-04-13 05:03:17 +0000 | [diff] [blame] | 164 | tab = rtnl_msg_handlers[protocol]; |
| 165 | else |
| 166 | tab = NULL; |
| 167 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 168 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 169 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 170 | |
Hans Zhang | c80bbea | 2012-10-22 22:21:23 +0000 | [diff] [blame] | 171 | return tab[msgindex].dumpit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 174 | static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) |
| 175 | { |
| 176 | struct rtnl_link *tab; |
| 177 | |
| 178 | if (protocol <= RTNL_FAMILY_MAX) |
| 179 | tab = rtnl_msg_handlers[protocol]; |
| 180 | else |
| 181 | tab = NULL; |
| 182 | |
| 183 | if (tab == NULL || tab[msgindex].calcit == NULL) |
| 184 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 185 | |
Hans Zhang | c80bbea | 2012-10-22 22:21:23 +0000 | [diff] [blame] | 186 | return tab[msgindex].calcit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 189 | /** |
| 190 | * __rtnl_register - Register a rtnetlink message type |
| 191 | * @protocol: Protocol family or PF_UNSPEC |
| 192 | * @msgtype: rtnetlink message type |
| 193 | * @doit: Function pointer called for each request message |
| 194 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 195 | * @calcit: Function pointer to calc size of dump message |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 196 | * |
| 197 | * Registers the specified function pointers (at least one of them has |
| 198 | * to be non-NULL) to be called whenever a request message for the |
| 199 | * specified protocol family and message type is received. |
| 200 | * |
| 201 | * The special protocol family PF_UNSPEC may be used to define fallback |
| 202 | * function pointers for the case when no entry for the specific protocol |
| 203 | * family exists. |
| 204 | * |
| 205 | * Returns 0 on success or a negative error code. |
| 206 | */ |
| 207 | int __rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 208 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 209 | rtnl_calcit_func calcit) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 210 | { |
| 211 | struct rtnl_link *tab; |
| 212 | int msgindex; |
| 213 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 214 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 215 | msgindex = rtm_msgindex(msgtype); |
| 216 | |
| 217 | tab = rtnl_msg_handlers[protocol]; |
| 218 | if (tab == NULL) { |
| 219 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); |
| 220 | if (tab == NULL) |
| 221 | return -ENOBUFS; |
| 222 | |
| 223 | rtnl_msg_handlers[protocol] = tab; |
| 224 | } |
| 225 | |
| 226 | if (doit) |
| 227 | tab[msgindex].doit = doit; |
| 228 | |
| 229 | if (dumpit) |
| 230 | tab[msgindex].dumpit = dumpit; |
| 231 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 232 | if (calcit) |
| 233 | tab[msgindex].calcit = calcit; |
| 234 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 235 | return 0; |
| 236 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(__rtnl_register); |
| 238 | |
| 239 | /** |
| 240 | * rtnl_register - Register a rtnetlink message type |
| 241 | * |
| 242 | * Identical to __rtnl_register() but panics on failure. This is useful |
| 243 | * as failure of this function is very unlikely, it can only happen due |
| 244 | * to lack of memory when allocating the chain to store all message |
| 245 | * handlers for a protocol. Meant for use in init functions where lack |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 246 | * of memory implies no sense in continuing. |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 247 | */ |
| 248 | void rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 249 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 250 | rtnl_calcit_func calcit) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 251 | { |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 252 | if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 253 | panic("Unable to register rtnetlink message handler, " |
| 254 | "protocol = %d, message type = %d\n", |
| 255 | protocol, msgtype); |
| 256 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(rtnl_register); |
| 258 | |
| 259 | /** |
| 260 | * rtnl_unregister - Unregister a rtnetlink message type |
| 261 | * @protocol: Protocol family or PF_UNSPEC |
| 262 | * @msgtype: rtnetlink message type |
| 263 | * |
| 264 | * Returns 0 on success or a negative error code. |
| 265 | */ |
| 266 | int rtnl_unregister(int protocol, int msgtype) |
| 267 | { |
| 268 | int msgindex; |
| 269 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 270 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 271 | msgindex = rtm_msgindex(msgtype); |
| 272 | |
| 273 | if (rtnl_msg_handlers[protocol] == NULL) |
| 274 | return -ENOENT; |
| 275 | |
| 276 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; |
| 277 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; |
| 278 | |
| 279 | return 0; |
| 280 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 281 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
| 282 | |
| 283 | /** |
| 284 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol |
| 285 | * @protocol : Protocol family or PF_UNSPEC |
| 286 | * |
| 287 | * Identical to calling rtnl_unregster() for all registered message types |
| 288 | * of a certain protocol family. |
| 289 | */ |
| 290 | void rtnl_unregister_all(int protocol) |
| 291 | { |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 292 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 293 | |
| 294 | kfree(rtnl_msg_handlers[protocol]); |
| 295 | rtnl_msg_handlers[protocol] = NULL; |
| 296 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 297 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 299 | static LIST_HEAD(link_ops); |
| 300 | |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 301 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
| 302 | { |
| 303 | const struct rtnl_link_ops *ops; |
| 304 | |
| 305 | list_for_each_entry(ops, &link_ops, list) { |
| 306 | if (!strcmp(ops->kind, kind)) |
| 307 | return ops; |
| 308 | } |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 312 | /** |
| 313 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 314 | * @ops: struct rtnl_link_ops * to register |
| 315 | * |
| 316 | * The caller must hold the rtnl_mutex. This function should be used |
| 317 | * by drivers that create devices during module initialization. It |
| 318 | * must be called before registering the devices. |
| 319 | * |
| 320 | * Returns 0 on success or a negative error code. |
| 321 | */ |
| 322 | int __rtnl_link_register(struct rtnl_link_ops *ops) |
| 323 | { |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 324 | if (rtnl_link_ops_get(ops->kind)) |
| 325 | return -EEXIST; |
| 326 | |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 327 | /* The check for setup is here because if ops |
| 328 | * does not have that filled up, it is not possible |
| 329 | * to use the ops for creating device. So do not |
| 330 | * fill up dellink as well. That disables rtnl_dellink. |
| 331 | */ |
| 332 | if (ops->setup && !ops->dellink) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 333 | ops->dellink = unregister_netdevice_queue; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 334 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 335 | list_add_tail(&ops->list, &link_ops); |
| 336 | return 0; |
| 337 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 338 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
| 339 | |
| 340 | /** |
| 341 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 342 | * @ops: struct rtnl_link_ops * to register |
| 343 | * |
| 344 | * Returns 0 on success or a negative error code. |
| 345 | */ |
| 346 | int rtnl_link_register(struct rtnl_link_ops *ops) |
| 347 | { |
| 348 | int err; |
| 349 | |
| 350 | rtnl_lock(); |
| 351 | err = __rtnl_link_register(ops); |
| 352 | rtnl_unlock(); |
| 353 | return err; |
| 354 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 355 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
| 356 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 357 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
| 358 | { |
| 359 | struct net_device *dev; |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 360 | LIST_HEAD(list_kill); |
| 361 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 362 | for_each_netdev(net, dev) { |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 363 | if (dev->rtnl_link_ops == ops) |
| 364 | ops->dellink(dev, &list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 365 | } |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 366 | unregister_netdevice_many(&list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 369 | /** |
| 370 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 371 | * @ops: struct rtnl_link_ops * to unregister |
| 372 | * |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 373 | * The caller must hold the rtnl_mutex. |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 374 | */ |
| 375 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 376 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 377 | struct net *net; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 378 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 379 | for_each_net(net) { |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 380 | __rtnl_kill_links(net, ops); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 381 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 382 | list_del(&ops->list); |
| 383 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 384 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
| 385 | |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 386 | /* Return with the rtnl_lock held when there are no network |
| 387 | * devices unregistering in any network namespace. |
| 388 | */ |
| 389 | static void rtnl_lock_unregistering_all(void) |
| 390 | { |
| 391 | struct net *net; |
| 392 | bool unregistering; |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 393 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 394 | |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 395 | add_wait_queue(&netdev_unregistering_wq, &wait); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 396 | for (;;) { |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 397 | unregistering = false; |
| 398 | rtnl_lock(); |
| 399 | for_each_net(net) { |
| 400 | if (net->dev_unreg_count > 0) { |
| 401 | unregistering = true; |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | if (!unregistering) |
| 406 | break; |
| 407 | __rtnl_unlock(); |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 408 | |
| 409 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 410 | } |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 411 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 414 | /** |
| 415 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 416 | * @ops: struct rtnl_link_ops * to unregister |
| 417 | */ |
| 418 | void rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 419 | { |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 420 | /* Close the race with cleanup_net() */ |
| 421 | mutex_lock(&net_mutex); |
| 422 | rtnl_lock_unregistering_all(); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 423 | __rtnl_link_unregister(ops); |
| 424 | rtnl_unlock(); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 425 | mutex_unlock(&net_mutex); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 426 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 427 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
| 428 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 429 | static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) |
| 430 | { |
| 431 | struct net_device *master_dev; |
| 432 | const struct rtnl_link_ops *ops; |
| 433 | |
| 434 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
| 435 | if (!master_dev) |
| 436 | return 0; |
| 437 | ops = master_dev->rtnl_link_ops; |
Fernando Luis Vazquez Cao | 6049f25 | 2014-02-04 19:35:02 +0900 | [diff] [blame] | 438 | if (!ops || !ops->get_slave_size) |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 439 | return 0; |
| 440 | /* IFLA_INFO_SLAVE_DATA + nested data */ |
| 441 | return nla_total_size(sizeof(struct nlattr)) + |
| 442 | ops->get_slave_size(master_dev, dev); |
| 443 | } |
| 444 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 445 | static size_t rtnl_link_get_size(const struct net_device *dev) |
| 446 | { |
| 447 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 448 | size_t size; |
| 449 | |
| 450 | if (!ops) |
| 451 | return 0; |
| 452 | |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 453 | size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
| 454 | nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 455 | |
| 456 | if (ops->get_size) |
| 457 | /* IFLA_INFO_DATA + nested data */ |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 458 | size += nla_total_size(sizeof(struct nlattr)) + |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 459 | ops->get_size(dev); |
| 460 | |
| 461 | if (ops->get_xstats_size) |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 462 | /* IFLA_INFO_XSTATS */ |
| 463 | size += nla_total_size(ops->get_xstats_size(dev)); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 464 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 465 | size += rtnl_link_get_slave_info_data_size(dev); |
| 466 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 467 | return size; |
| 468 | } |
| 469 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 470 | static LIST_HEAD(rtnl_af_ops); |
| 471 | |
| 472 | static const struct rtnl_af_ops *rtnl_af_lookup(const int family) |
| 473 | { |
| 474 | const struct rtnl_af_ops *ops; |
| 475 | |
| 476 | list_for_each_entry(ops, &rtnl_af_ops, list) { |
| 477 | if (ops->family == family) |
| 478 | return ops; |
| 479 | } |
| 480 | |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | /** |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 485 | * rtnl_af_register - Register rtnl_af_ops with rtnetlink. |
| 486 | * @ops: struct rtnl_af_ops * to register |
| 487 | * |
| 488 | * Returns 0 on success or a negative error code. |
| 489 | */ |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 490 | void rtnl_af_register(struct rtnl_af_ops *ops) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 491 | { |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 492 | rtnl_lock(); |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 493 | list_add_tail(&ops->list, &rtnl_af_ops); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 494 | rtnl_unlock(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 495 | } |
| 496 | EXPORT_SYMBOL_GPL(rtnl_af_register); |
| 497 | |
| 498 | /** |
| 499 | * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 500 | * @ops: struct rtnl_af_ops * to unregister |
| 501 | * |
| 502 | * The caller must hold the rtnl_mutex. |
| 503 | */ |
| 504 | void __rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 505 | { |
| 506 | list_del(&ops->list); |
| 507 | } |
| 508 | EXPORT_SYMBOL_GPL(__rtnl_af_unregister); |
| 509 | |
| 510 | /** |
| 511 | * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 512 | * @ops: struct rtnl_af_ops * to unregister |
| 513 | */ |
| 514 | void rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 515 | { |
| 516 | rtnl_lock(); |
| 517 | __rtnl_af_unregister(ops); |
| 518 | rtnl_unlock(); |
| 519 | } |
| 520 | EXPORT_SYMBOL_GPL(rtnl_af_unregister); |
| 521 | |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 522 | static size_t rtnl_link_get_af_size(const struct net_device *dev, |
| 523 | u32 ext_filter_mask) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 524 | { |
| 525 | struct rtnl_af_ops *af_ops; |
| 526 | size_t size; |
| 527 | |
| 528 | /* IFLA_AF_SPEC */ |
| 529 | size = nla_total_size(sizeof(struct nlattr)); |
| 530 | |
| 531 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 532 | if (af_ops->get_link_af_size) { |
| 533 | /* AF_* + nested data */ |
| 534 | size += nla_total_size(sizeof(struct nlattr)) + |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 535 | af_ops->get_link_af_size(dev, ext_filter_mask); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | return size; |
| 540 | } |
| 541 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 542 | static bool rtnl_have_link_slave_info(const struct net_device *dev) |
| 543 | { |
| 544 | struct net_device *master_dev; |
| 545 | |
| 546 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
Jiri Pirko | 813f020 | 2014-01-23 19:19:21 +0100 | [diff] [blame] | 547 | if (master_dev && master_dev->rtnl_link_ops) |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 548 | return true; |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | static int rtnl_link_slave_info_fill(struct sk_buff *skb, |
| 553 | const struct net_device *dev) |
| 554 | { |
| 555 | struct net_device *master_dev; |
| 556 | const struct rtnl_link_ops *ops; |
| 557 | struct nlattr *slave_data; |
| 558 | int err; |
| 559 | |
| 560 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
| 561 | if (!master_dev) |
| 562 | return 0; |
| 563 | ops = master_dev->rtnl_link_ops; |
| 564 | if (!ops) |
| 565 | return 0; |
| 566 | if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) |
| 567 | return -EMSGSIZE; |
| 568 | if (ops->fill_slave_info) { |
| 569 | slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); |
| 570 | if (!slave_data) |
| 571 | return -EMSGSIZE; |
| 572 | err = ops->fill_slave_info(skb, master_dev, dev); |
| 573 | if (err < 0) |
| 574 | goto err_cancel_slave_data; |
| 575 | nla_nest_end(skb, slave_data); |
| 576 | } |
| 577 | return 0; |
| 578 | |
| 579 | err_cancel_slave_data: |
| 580 | nla_nest_cancel(skb, slave_data); |
| 581 | return err; |
| 582 | } |
| 583 | |
| 584 | static int rtnl_link_info_fill(struct sk_buff *skb, |
| 585 | const struct net_device *dev) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 586 | { |
| 587 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 588 | struct nlattr *data; |
| 589 | int err; |
| 590 | |
| 591 | if (!ops) |
| 592 | return 0; |
| 593 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) |
| 594 | return -EMSGSIZE; |
| 595 | if (ops->fill_xstats) { |
| 596 | err = ops->fill_xstats(skb, dev); |
| 597 | if (err < 0) |
| 598 | return err; |
| 599 | } |
| 600 | if (ops->fill_info) { |
| 601 | data = nla_nest_start(skb, IFLA_INFO_DATA); |
| 602 | if (data == NULL) |
| 603 | return -EMSGSIZE; |
| 604 | err = ops->fill_info(skb, dev); |
| 605 | if (err < 0) |
| 606 | goto err_cancel_data; |
| 607 | nla_nest_end(skb, data); |
| 608 | } |
| 609 | return 0; |
| 610 | |
| 611 | err_cancel_data: |
| 612 | nla_nest_cancel(skb, data); |
| 613 | return err; |
| 614 | } |
| 615 | |
| 616 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) |
| 617 | { |
| 618 | struct nlattr *linkinfo; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 619 | int err = -EMSGSIZE; |
| 620 | |
| 621 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); |
| 622 | if (linkinfo == NULL) |
| 623 | goto out; |
| 624 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 625 | err = rtnl_link_info_fill(skb, dev); |
| 626 | if (err < 0) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 627 | goto err_cancel_link; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 628 | |
| 629 | err = rtnl_link_slave_info_fill(skb, dev); |
| 630 | if (err < 0) |
| 631 | goto err_cancel_link; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 632 | |
| 633 | nla_nest_end(skb, linkinfo); |
| 634 | return 0; |
| 635 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 636 | err_cancel_link: |
| 637 | nla_nest_cancel(skb, linkinfo); |
| 638 | out: |
| 639 | return err; |
| 640 | } |
| 641 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 642 | int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 644 | struct sock *rtnl = net->rtnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | int err = 0; |
| 646 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 647 | NETLINK_CB(skb).dst_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (echo) |
| 649 | atomic_inc(&skb->users); |
| 650 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
| 651 | if (echo) |
| 652 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); |
| 653 | return err; |
| 654 | } |
| 655 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 656 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 657 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 658 | struct sock *rtnl = net->rtnl; |
| 659 | |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 660 | return nlmsg_unicast(rtnl, skb, pid); |
| 661 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 662 | EXPORT_SYMBOL(rtnl_unicast); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 663 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 664 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
| 665 | struct nlmsghdr *nlh, gfp_t flags) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 666 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 667 | struct sock *rtnl = net->rtnl; |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 668 | int report = 0; |
| 669 | |
| 670 | if (nlh) |
| 671 | report = nlmsg_report(nlh); |
| 672 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 673 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 674 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 675 | EXPORT_SYMBOL(rtnl_notify); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 676 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 677 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 678 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 679 | struct sock *rtnl = net->rtnl; |
| 680 | |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 681 | netlink_set_err(rtnl, 0, group, error); |
| 682 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 683 | EXPORT_SYMBOL(rtnl_set_sk_err); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 686 | { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 687 | struct nlattr *mx; |
| 688 | int i, valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 690 | mx = nla_nest_start(skb, RTA_METRICS); |
| 691 | if (mx == NULL) |
| 692 | return -ENOBUFS; |
| 693 | |
| 694 | for (i = 0; i < RTAX_MAX; i++) { |
| 695 | if (metrics[i]) { |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 696 | if (i == RTAX_CC_ALGO - 1) { |
| 697 | char tmp[TCP_CA_NAME_MAX], *name; |
| 698 | |
| 699 | name = tcp_ca_get_name_by_key(metrics[i], tmp); |
| 700 | if (!name) |
| 701 | continue; |
| 702 | if (nla_put_string(skb, i + 1, name)) |
| 703 | goto nla_put_failure; |
Daniel Borkmann | c3a8d94 | 2015-08-31 15:58:47 +0200 | [diff] [blame] | 704 | } else if (i == RTAX_FEATURES - 1) { |
| 705 | u32 user_features = metrics[i] & RTAX_FEATURE_MASK; |
| 706 | |
| 707 | BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); |
| 708 | if (nla_put_u32(skb, i + 1, user_features)) |
| 709 | goto nla_put_failure; |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 710 | } else { |
| 711 | if (nla_put_u32(skb, i + 1, metrics[i])) |
| 712 | goto nla_put_failure; |
| 713 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 714 | valid++; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
David S. Miller | a57d27f | 2006-08-22 22:20:14 -0700 | [diff] [blame] | 718 | if (!valid) { |
| 719 | nla_nest_cancel(skb, mx); |
| 720 | return 0; |
| 721 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 722 | |
| 723 | return nla_nest_end(skb, mx); |
| 724 | |
| 725 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 726 | nla_nest_cancel(skb, mx); |
| 727 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 729 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 731 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
David S. Miller | 87a5069 | 2012-07-10 05:06:14 -0700 | [diff] [blame] | 732 | long expires, u32 error) |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 733 | { |
| 734 | struct rta_cacheinfo ci = { |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 735 | .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 736 | .rta_used = dst->__use, |
| 737 | .rta_clntref = atomic_read(&(dst->__refcnt)), |
| 738 | .rta_error = error, |
| 739 | .rta_id = id, |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 740 | }; |
| 741 | |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 742 | if (expires) { |
| 743 | unsigned long clock; |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 744 | |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 745 | clock = jiffies_to_clock_t(abs(expires)); |
| 746 | clock = min_t(unsigned long, clock, INT_MAX); |
| 747 | ci.rta_expires = (expires > 0) ? clock : -clock; |
| 748 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 749 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
| 750 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 751 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 753 | static void set_operstate(struct net_device *dev, unsigned char transition) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 754 | { |
| 755 | unsigned char operstate = dev->operstate; |
| 756 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 757 | switch (transition) { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 758 | case IF_OPER_UP: |
| 759 | if ((operstate == IF_OPER_DORMANT || |
| 760 | operstate == IF_OPER_UNKNOWN) && |
| 761 | !netif_dormant(dev)) |
| 762 | operstate = IF_OPER_UP; |
| 763 | break; |
| 764 | |
| 765 | case IF_OPER_DORMANT: |
| 766 | if (operstate == IF_OPER_UP || |
| 767 | operstate == IF_OPER_UNKNOWN) |
| 768 | operstate = IF_OPER_DORMANT; |
| 769 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 770 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 771 | |
| 772 | if (dev->operstate != operstate) { |
| 773 | write_lock_bh(&dev_base_lock); |
| 774 | dev->operstate = operstate; |
| 775 | write_unlock_bh(&dev_base_lock); |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 776 | netdev_state_change(dev); |
| 777 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Jiri Benc | b1beb68 | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 780 | static unsigned int rtnl_dev_get_flags(const struct net_device *dev) |
| 781 | { |
| 782 | return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | |
| 783 | (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); |
| 784 | } |
| 785 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 786 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
| 787 | const struct ifinfomsg *ifm) |
| 788 | { |
| 789 | unsigned int flags = ifm->ifi_flags; |
| 790 | |
| 791 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ |
| 792 | if (ifm->ifi_change) |
| 793 | flags = (flags & ifm->ifi_change) | |
Jiri Benc | b1beb68 | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 794 | (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 795 | |
| 796 | return flags; |
| 797 | } |
| 798 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 799 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 800 | const struct rtnl_link_stats64 *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 802 | a->rx_packets = b->rx_packets; |
| 803 | a->tx_packets = b->tx_packets; |
| 804 | a->rx_bytes = b->rx_bytes; |
| 805 | a->tx_bytes = b->tx_bytes; |
| 806 | a->rx_errors = b->rx_errors; |
| 807 | a->tx_errors = b->tx_errors; |
| 808 | a->rx_dropped = b->rx_dropped; |
| 809 | a->tx_dropped = b->tx_dropped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 811 | a->multicast = b->multicast; |
| 812 | a->collisions = b->collisions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 814 | a->rx_length_errors = b->rx_length_errors; |
| 815 | a->rx_over_errors = b->rx_over_errors; |
| 816 | a->rx_crc_errors = b->rx_crc_errors; |
| 817 | a->rx_frame_errors = b->rx_frame_errors; |
| 818 | a->rx_fifo_errors = b->rx_fifo_errors; |
| 819 | a->rx_missed_errors = b->rx_missed_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 821 | a->tx_aborted_errors = b->tx_aborted_errors; |
| 822 | a->tx_carrier_errors = b->tx_carrier_errors; |
| 823 | a->tx_fifo_errors = b->tx_fifo_errors; |
| 824 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; |
| 825 | a->tx_window_errors = b->tx_window_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 827 | a->rx_compressed = b->rx_compressed; |
| 828 | a->tx_compressed = b->tx_compressed; |
Jarod Wilson | 6e7333d | 2016-02-01 18:51:05 -0500 | [diff] [blame] | 829 | |
| 830 | a->rx_nohandler = b->rx_nohandler; |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 833 | /* All VF info */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 834 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
| 835 | u32 ext_filter_mask) |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 836 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 837 | if (dev->dev.parent && dev_is_pci(dev->dev.parent) && |
| 838 | (ext_filter_mask & RTEXT_FILTER_VF)) { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 839 | int num_vfs = dev_num_vf(dev->dev.parent); |
Scott Feldman | 045de01 | 2010-05-28 03:42:43 -0700 | [diff] [blame] | 840 | size_t size = nla_total_size(sizeof(struct nlattr)); |
| 841 | size += nla_total_size(num_vfs * sizeof(struct nlattr)); |
| 842 | size += num_vfs * |
| 843 | (nla_total_size(sizeof(struct ifla_vf_mac)) + |
| 844 | nla_total_size(sizeof(struct ifla_vf_vlan)) + |
Sucheta Chakraborty | ed61668 | 2014-05-22 09:59:05 -0400 | [diff] [blame] | 845 | nla_total_size(sizeof(struct ifla_vf_spoofchk)) + |
Jiri Benc | 945a367 | 2014-08-08 16:44:32 +0200 | [diff] [blame] | 846 | nla_total_size(sizeof(struct ifla_vf_rate)) + |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 847 | nla_total_size(sizeof(struct ifla_vf_link_state)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 848 | nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + |
| 849 | /* IFLA_VF_STATS_RX_PACKETS */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 850 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 851 | /* IFLA_VF_STATS_TX_PACKETS */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 852 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 853 | /* IFLA_VF_STATS_RX_BYTES */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 854 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 855 | /* IFLA_VF_STATS_TX_BYTES */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 856 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 857 | /* IFLA_VF_STATS_BROADCAST */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 858 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 859 | /* IFLA_VF_STATS_MULTICAST */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 860 | nla_total_size_64bit(sizeof(__u64)) + |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 861 | nla_total_size(sizeof(struct ifla_vf_trust))); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 862 | return size; |
| 863 | } else |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 867 | static size_t rtnl_port_size(const struct net_device *dev, |
| 868 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 869 | { |
| 870 | size_t port_size = nla_total_size(4) /* PORT_VF */ |
| 871 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ |
| 872 | + nla_total_size(sizeof(struct ifla_port_vsi)) |
| 873 | /* PORT_VSI_TYPE */ |
| 874 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ |
| 875 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ |
| 876 | + nla_total_size(1) /* PROT_VDP_REQUEST */ |
| 877 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ |
| 878 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); |
| 879 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) |
| 880 | + port_size; |
| 881 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) |
| 882 | + port_size; |
| 883 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 884 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 885 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 886 | return 0; |
| 887 | if (dev_num_vf(dev->dev.parent)) |
| 888 | return port_self_size + vf_ports_size + |
| 889 | vf_port_size * dev_num_vf(dev->dev.parent); |
| 890 | else |
| 891 | return port_self_size; |
| 892 | } |
| 893 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 894 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
| 895 | u32 ext_filter_mask) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 896 | { |
| 897 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 898 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 899 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 900 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
Nicolas Dichtel | 270cb4d | 2016-04-26 10:06:16 +0200 | [diff] [blame] | 901 | + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 902 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
David S. Miller | 35c5845 | 2016-04-19 19:49:29 -0400 | [diff] [blame] | 903 | + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 904 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 905 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 906 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 907 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 908 | + nla_total_size(4) /* IFLA_MTU */ |
| 909 | + nla_total_size(4) /* IFLA_LINK */ |
| 910 | + nla_total_size(4) /* IFLA_MASTER */ |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 911 | + nla_total_size(1) /* IFLA_CARRIER */ |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 912 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 913 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
| 914 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 915 | + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */ |
| 916 | + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 917 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 918 | + nla_total_size(1) /* IFLA_LINKMODE */ |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 919 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 920 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 921 | + nla_total_size(ext_filter_mask |
| 922 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ |
| 923 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 924 | + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 925 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 926 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 927 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 928 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
Nicolas Dichtel | c57c7a9 | 2016-03-31 18:10:31 +0200 | [diff] [blame] | 929 | + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 930 | + nla_total_size(1); /* IFLA_PROTO_DOWN */ |
| 931 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 934 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
| 935 | { |
| 936 | struct nlattr *vf_ports; |
| 937 | struct nlattr *vf_port; |
| 938 | int vf; |
| 939 | int err; |
| 940 | |
| 941 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); |
| 942 | if (!vf_ports) |
| 943 | return -EMSGSIZE; |
| 944 | |
| 945 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { |
| 946 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 947 | if (!vf_port) |
| 948 | goto nla_put_failure; |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 949 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
| 950 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 951 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 952 | if (err == -EMSGSIZE) |
| 953 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 954 | if (err) { |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 955 | nla_nest_cancel(skb, vf_port); |
| 956 | continue; |
| 957 | } |
| 958 | nla_nest_end(skb, vf_port); |
| 959 | } |
| 960 | |
| 961 | nla_nest_end(skb, vf_ports); |
| 962 | |
| 963 | return 0; |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 964 | |
| 965 | nla_put_failure: |
| 966 | nla_nest_cancel(skb, vf_ports); |
| 967 | return -EMSGSIZE; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) |
| 971 | { |
| 972 | struct nlattr *port_self; |
| 973 | int err; |
| 974 | |
| 975 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); |
| 976 | if (!port_self) |
| 977 | return -EMSGSIZE; |
| 978 | |
| 979 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); |
| 980 | if (err) { |
| 981 | nla_nest_cancel(skb, port_self); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 982 | return (err == -EMSGSIZE) ? err : 0; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | nla_nest_end(skb, port_self); |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 990 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
| 991 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 992 | { |
| 993 | int err; |
| 994 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 995 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 996 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 997 | return 0; |
| 998 | |
| 999 | err = rtnl_port_self_fill(skb, dev); |
| 1000 | if (err) |
| 1001 | return err; |
| 1002 | |
| 1003 | if (dev_num_vf(dev->dev.parent)) { |
| 1004 | err = rtnl_vf_ports_fill(skb, dev); |
| 1005 | if (err) |
| 1006 | return err; |
| 1007 | } |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1012 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1013 | { |
| 1014 | int err; |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1015 | struct netdev_phys_item_id ppid; |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1016 | |
| 1017 | err = dev_get_phys_port_id(dev, &ppid); |
| 1018 | if (err) { |
| 1019 | if (err == -EOPNOTSUPP) |
| 1020 | return 0; |
| 1021 | return err; |
| 1022 | } |
| 1023 | |
| 1024 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) |
| 1025 | return -EMSGSIZE; |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1030 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
| 1031 | { |
| 1032 | char name[IFNAMSIZ]; |
| 1033 | int err; |
| 1034 | |
| 1035 | err = dev_get_phys_port_name(dev, name, sizeof(name)); |
| 1036 | if (err) { |
| 1037 | if (err == -EOPNOTSUPP) |
| 1038 | return 0; |
| 1039 | return err; |
| 1040 | } |
| 1041 | |
| 1042 | if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name)) |
| 1043 | return -EMSGSIZE; |
| 1044 | |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1048 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1049 | { |
| 1050 | int err; |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1051 | struct switchdev_attr attr = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 1052 | .orig_dev = dev, |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1053 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1054 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1055 | }; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1056 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1057 | err = switchdev_port_attr_get(dev, &attr); |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1058 | if (err) { |
| 1059 | if (err == -EOPNOTSUPP) |
| 1060 | return 0; |
| 1061 | return err; |
| 1062 | } |
| 1063 | |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 1064 | if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, |
| 1065 | attr.u.ppid.id)) |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1066 | return -EMSGSIZE; |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1071 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
| 1072 | struct net_device *dev) |
| 1073 | { |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1074 | struct rtnl_link_stats64 *sp; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1075 | struct nlattr *attr; |
| 1076 | |
Nicolas Dichtel | 58414d3 | 2016-04-21 18:58:25 +0200 | [diff] [blame] | 1077 | attr = nla_reserve_64bit(skb, IFLA_STATS64, |
| 1078 | sizeof(struct rtnl_link_stats64), IFLA_PAD); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1079 | if (!attr) |
| 1080 | return -EMSGSIZE; |
| 1081 | |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1082 | sp = nla_data(attr); |
| 1083 | dev_get_stats(dev, sp); |
| 1084 | |
| 1085 | attr = nla_reserve(skb, IFLA_STATS, |
| 1086 | sizeof(struct rtnl_link_stats)); |
| 1087 | if (!attr) |
| 1088 | return -EMSGSIZE; |
| 1089 | |
| 1090 | copy_rtnl_link_stats(nla_data(attr), sp); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, |
| 1096 | struct net_device *dev, |
| 1097 | int vfs_num, |
| 1098 | struct nlattr *vfinfo) |
| 1099 | { |
| 1100 | struct ifla_vf_rss_query_en vf_rss_query_en; |
| 1101 | struct ifla_vf_link_state vf_linkstate; |
| 1102 | struct ifla_vf_spoofchk vf_spoofchk; |
| 1103 | struct ifla_vf_tx_rate vf_tx_rate; |
| 1104 | struct ifla_vf_stats vf_stats; |
| 1105 | struct ifla_vf_trust vf_trust; |
| 1106 | struct ifla_vf_vlan vf_vlan; |
| 1107 | struct ifla_vf_rate vf_rate; |
| 1108 | struct nlattr *vf, *vfstats; |
| 1109 | struct ifla_vf_mac vf_mac; |
| 1110 | struct ifla_vf_info ivi; |
| 1111 | |
| 1112 | /* Not all SR-IOV capable drivers support the |
| 1113 | * spoofcheck and "RSS query enable" query. Preset to |
| 1114 | * -1 so the user space tool can detect that the driver |
| 1115 | * didn't report anything. |
| 1116 | */ |
| 1117 | ivi.spoofchk = -1; |
| 1118 | ivi.rss_query_en = -1; |
| 1119 | ivi.trusted = -1; |
| 1120 | memset(ivi.mac, 0, sizeof(ivi.mac)); |
| 1121 | /* The default value for VF link state is "auto" |
| 1122 | * IFLA_VF_LINK_STATE_AUTO which equals zero |
| 1123 | */ |
| 1124 | ivi.linkstate = 0; |
| 1125 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) |
| 1126 | return 0; |
| 1127 | |
| 1128 | vf_mac.vf = |
| 1129 | vf_vlan.vf = |
| 1130 | vf_rate.vf = |
| 1131 | vf_tx_rate.vf = |
| 1132 | vf_spoofchk.vf = |
| 1133 | vf_linkstate.vf = |
| 1134 | vf_rss_query_en.vf = |
| 1135 | vf_trust.vf = ivi.vf; |
| 1136 | |
| 1137 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
| 1138 | vf_vlan.vlan = ivi.vlan; |
| 1139 | vf_vlan.qos = ivi.qos; |
| 1140 | vf_tx_rate.rate = ivi.max_tx_rate; |
| 1141 | vf_rate.min_tx_rate = ivi.min_tx_rate; |
| 1142 | vf_rate.max_tx_rate = ivi.max_tx_rate; |
| 1143 | vf_spoofchk.setting = ivi.spoofchk; |
| 1144 | vf_linkstate.link_state = ivi.linkstate; |
| 1145 | vf_rss_query_en.setting = ivi.rss_query_en; |
| 1146 | vf_trust.setting = ivi.trusted; |
| 1147 | vf = nla_nest_start(skb, IFLA_VF_INFO); |
| 1148 | if (!vf) { |
| 1149 | nla_nest_cancel(skb, vfinfo); |
| 1150 | return -EMSGSIZE; |
| 1151 | } |
| 1152 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
| 1153 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || |
| 1154 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), |
| 1155 | &vf_rate) || |
| 1156 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), |
| 1157 | &vf_tx_rate) || |
| 1158 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), |
| 1159 | &vf_spoofchk) || |
| 1160 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), |
| 1161 | &vf_linkstate) || |
| 1162 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, |
| 1163 | sizeof(vf_rss_query_en), |
| 1164 | &vf_rss_query_en) || |
| 1165 | nla_put(skb, IFLA_VF_TRUST, |
| 1166 | sizeof(vf_trust), &vf_trust)) |
| 1167 | return -EMSGSIZE; |
| 1168 | memset(&vf_stats, 0, sizeof(vf_stats)); |
| 1169 | if (dev->netdev_ops->ndo_get_vf_stats) |
| 1170 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, |
| 1171 | &vf_stats); |
| 1172 | vfstats = nla_nest_start(skb, IFLA_VF_STATS); |
| 1173 | if (!vfstats) { |
| 1174 | nla_nest_cancel(skb, vf); |
| 1175 | nla_nest_cancel(skb, vfinfo); |
| 1176 | return -EMSGSIZE; |
| 1177 | } |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 1178 | if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, |
| 1179 | vf_stats.rx_packets, IFLA_VF_STATS_PAD) || |
| 1180 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, |
| 1181 | vf_stats.tx_packets, IFLA_VF_STATS_PAD) || |
| 1182 | nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, |
| 1183 | vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || |
| 1184 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, |
| 1185 | vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || |
| 1186 | nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, |
| 1187 | vf_stats.broadcast, IFLA_VF_STATS_PAD) || |
| 1188 | nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, |
| 1189 | vf_stats.multicast, IFLA_VF_STATS_PAD)) |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1190 | return -EMSGSIZE; |
| 1191 | nla_nest_end(skb, vfstats); |
| 1192 | nla_nest_end(skb, vf); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) |
| 1197 | { |
Kangjie Lu | 5f8e447 | 2016-05-03 16:46:24 -0400 | [diff] [blame] | 1198 | struct rtnl_link_ifmap map; |
| 1199 | |
| 1200 | memset(&map, 0, sizeof(map)); |
| 1201 | map.mem_start = dev->mem_start; |
| 1202 | map.mem_end = dev->mem_end; |
| 1203 | map.base_addr = dev->base_addr; |
| 1204 | map.irq = dev->irq; |
| 1205 | map.dma = dev->dma; |
| 1206 | map.port = dev->if_port; |
| 1207 | |
Nicolas Dichtel | 270cb4d | 2016-04-26 10:06:16 +0200 | [diff] [blame] | 1208 | if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1209 | return -EMSGSIZE; |
| 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1214 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1215 | int type, u32 pid, u32 seq, u32 change, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1216 | unsigned int flags, u32 ext_filter_mask) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1217 | { |
| 1218 | struct ifinfomsg *ifm; |
| 1219 | struct nlmsghdr *nlh; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1220 | struct nlattr *af_spec; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1221 | struct rtnl_af_ops *af_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1222 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1223 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 1224 | ASSERT_RTNL(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1225 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 1226 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1227 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1228 | |
| 1229 | ifm = nlmsg_data(nlh); |
| 1230 | ifm->ifi_family = AF_UNSPEC; |
| 1231 | ifm->__ifi_pad = 0; |
| 1232 | ifm->ifi_type = dev->type; |
| 1233 | ifm->ifi_index = dev->ifindex; |
| 1234 | ifm->ifi_flags = dev_get_flags(dev); |
| 1235 | ifm->ifi_change = change; |
| 1236 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1237 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 1238 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || |
| 1239 | nla_put_u8(skb, IFLA_OPERSTATE, |
| 1240 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || |
| 1241 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || |
| 1242 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 1243 | nla_put_u32(skb, IFLA_GROUP, dev->group) || |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 1244 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1245 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 1246 | nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || |
| 1247 | nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || |
Mark A. Greer | 1d69c2b | 2012-07-20 13:35:13 +0000 | [diff] [blame] | 1248 | #ifdef CONFIG_RPS |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1249 | nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || |
Mark A. Greer | 1d69c2b | 2012-07-20 13:35:13 +0000 | [diff] [blame] | 1250 | #endif |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 1251 | (dev->ifindex != dev_get_iflink(dev) && |
| 1252 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1253 | (upper_dev && |
| 1254 | nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1255 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1256 | (dev->qdisc && |
| 1257 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || |
| 1258 | (dev->ifalias && |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1259 | nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || |
| 1260 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1261 | atomic_read(&dev->carrier_changes)) || |
| 1262 | nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1263 | goto nla_put_failure; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1264 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1265 | if (rtnl_fill_link_ifmap(skb, dev)) |
| 1266 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | if (dev->addr_len) { |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1269 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
| 1270 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) |
| 1271 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1274 | if (rtnl_phys_port_id_fill(skb, dev)) |
| 1275 | goto nla_put_failure; |
| 1276 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1277 | if (rtnl_phys_port_name_fill(skb, dev)) |
| 1278 | goto nla_put_failure; |
| 1279 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1280 | if (rtnl_phys_switch_id_fill(skb, dev)) |
| 1281 | goto nla_put_failure; |
| 1282 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1283 | if (rtnl_fill_stats(skb, dev)) |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 1284 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1286 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && |
| 1287 | nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) |
| 1288 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1289 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1290 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent && |
| 1291 | ext_filter_mask & RTEXT_FILTER_VF) { |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1292 | int i; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1293 | struct nlattr *vfinfo; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1294 | int num_vfs = dev_num_vf(dev->dev.parent); |
| 1295 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1296 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
| 1297 | if (!vfinfo) |
| 1298 | goto nla_put_failure; |
| 1299 | for (i = 0; i < num_vfs; i++) { |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1300 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1301 | goto nla_put_failure; |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1302 | } |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1303 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1304 | nla_nest_end(skb, vfinfo); |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1305 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1306 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1307 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1308 | goto nla_put_failure; |
| 1309 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1310 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1311 | if (rtnl_link_fill(skb, dev) < 0) |
| 1312 | goto nla_put_failure; |
| 1313 | } |
| 1314 | |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1315 | if (dev->rtnl_link_ops && |
| 1316 | dev->rtnl_link_ops->get_link_net) { |
| 1317 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); |
| 1318 | |
| 1319 | if (!net_eq(dev_net(dev), link_net)) { |
Nicolas Dichtel | 7a0877d | 2015-05-07 11:02:49 +0200 | [diff] [blame] | 1320 | int id = peernet2id_alloc(dev_net(dev), link_net); |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) |
| 1323 | goto nla_put_failure; |
| 1324 | } |
| 1325 | } |
| 1326 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1327 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
| 1328 | goto nla_put_failure; |
| 1329 | |
| 1330 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 1331 | if (af_ops->fill_link_af) { |
| 1332 | struct nlattr *af; |
| 1333 | int err; |
| 1334 | |
| 1335 | if (!(af = nla_nest_start(skb, af_ops->family))) |
| 1336 | goto nla_put_failure; |
| 1337 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 1338 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1339 | |
| 1340 | /* |
| 1341 | * Caller may return ENODATA to indicate that there |
| 1342 | * was no data to be dumped. This is not an error, it |
| 1343 | * means we should trim the attribute header and |
| 1344 | * continue. |
| 1345 | */ |
| 1346 | if (err == -ENODATA) |
| 1347 | nla_nest_cancel(skb, af); |
| 1348 | else if (err < 0) |
| 1349 | goto nla_put_failure; |
| 1350 | |
| 1351 | nla_nest_end(skb, af); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | nla_nest_end(skb, af_spec); |
| 1356 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1357 | nlmsg_end(skb, nlh); |
| 1358 | return 0; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1359 | |
| 1360 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1361 | nlmsg_cancel(skb, nlh); |
| 1362 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1365 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
| 1366 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
| 1367 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1368 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1369 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
| 1370 | [IFLA_MTU] = { .type = NLA_U32 }, |
| 1371 | [IFLA_LINK] = { .type = NLA_U32 }, |
| 1372 | [IFLA_MASTER] = { .type = NLA_U32 }, |
| 1373 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
| 1374 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 1375 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 1376 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 1377 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
| 1378 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
| 1379 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
| 1380 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
| 1381 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
| 1382 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
| 1383 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
| 1384 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, |
| 1385 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
| 1386 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
| 1387 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
| 1388 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
| 1389 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1390 | [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1391 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1392 | [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 1393 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1394 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1395 | }; |
| 1396 | |
| 1397 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 1398 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 1399 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 1400 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
| 1401 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, |
| 1402 | }; |
| 1403 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1404 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1405 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
| 1406 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, |
| 1407 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, |
| 1408 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, |
| 1409 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, |
| 1410 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1411 | [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1412 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1413 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1414 | [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
| 1415 | [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1416 | }; |
| 1417 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1418 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
| 1419 | [IFLA_PORT_VF] = { .type = NLA_U32 }, |
| 1420 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, |
| 1421 | .len = PORT_PROFILE_MAX }, |
| 1422 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, |
| 1423 | .len = sizeof(struct ifla_port_vsi)}, |
| 1424 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
| 1425 | .len = PORT_UUID_MAX }, |
| 1426 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, |
| 1427 | .len = PORT_UUID_MAX }, |
| 1428 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, |
| 1429 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, |
| 1430 | }; |
| 1431 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1432 | static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) |
| 1433 | { |
| 1434 | const struct rtnl_link_ops *ops = NULL; |
| 1435 | struct nlattr *linfo[IFLA_INFO_MAX + 1]; |
| 1436 | |
| 1437 | if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0) |
| 1438 | return NULL; |
| 1439 | |
| 1440 | if (linfo[IFLA_INFO_KIND]) { |
| 1441 | char kind[MODULE_NAME_LEN]; |
| 1442 | |
| 1443 | nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); |
| 1444 | ops = rtnl_link_ops_get(kind); |
| 1445 | } |
| 1446 | |
| 1447 | return ops; |
| 1448 | } |
| 1449 | |
| 1450 | static bool link_master_filtered(struct net_device *dev, int master_idx) |
| 1451 | { |
| 1452 | struct net_device *master; |
| 1453 | |
| 1454 | if (!master_idx) |
| 1455 | return false; |
| 1456 | |
| 1457 | master = netdev_master_upper_dev_get(dev); |
| 1458 | if (!master || master->ifindex != master_idx) |
| 1459 | return true; |
| 1460 | |
| 1461 | return false; |
| 1462 | } |
| 1463 | |
| 1464 | static bool link_kind_filtered(const struct net_device *dev, |
| 1465 | const struct rtnl_link_ops *kind_ops) |
| 1466 | { |
| 1467 | if (kind_ops && dev->rtnl_link_ops != kind_ops) |
| 1468 | return true; |
| 1469 | |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | static bool link_dump_filtered(struct net_device *dev, |
| 1474 | int master_idx, |
| 1475 | const struct rtnl_link_ops *kind_ops) |
| 1476 | { |
| 1477 | if (link_master_filtered(dev, master_idx) || |
| 1478 | link_kind_filtered(dev, kind_ops)) |
| 1479 | return true; |
| 1480 | |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1484 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1486 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1487 | int h, s_h; |
| 1488 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1490 | struct hlist_head *head; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1491 | struct nlattr *tb[IFLA_MAX+1]; |
| 1492 | u32 ext_filter_mask = 0; |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1493 | const struct rtnl_link_ops *kind_ops = NULL; |
| 1494 | unsigned int flags = NLM_F_MULTI; |
| 1495 | int master_idx = 0; |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1496 | int err; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 1497 | int hdrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1499 | s_h = cb->args[0]; |
| 1500 | s_idx = cb->args[1]; |
| 1501 | |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1502 | cb->seq = net->dev_base_seq; |
| 1503 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 1504 | /* A hack to preserve kernel<->userspace interface. |
| 1505 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. |
| 1506 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's |
| 1507 | * what iproute2 < v3.9.0 used. |
| 1508 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK |
| 1509 | * attribute, its netlink message is shorter than struct ifinfomsg. |
| 1510 | */ |
| 1511 | hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? |
| 1512 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 1513 | |
| 1514 | if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1515 | |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1516 | if (tb[IFLA_EXT_MASK]) |
| 1517 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1518 | |
| 1519 | if (tb[IFLA_MASTER]) |
| 1520 | master_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 1521 | |
| 1522 | if (tb[IFLA_LINKINFO]) |
| 1523 | kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); |
| 1524 | |
| 1525 | if (master_idx || kind_ops) |
| 1526 | flags |= NLM_F_DUMP_FILTERED; |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1527 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1528 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1529 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 1530 | idx = 0; |
| 1531 | head = &net->dev_index_head[h]; |
Eric Dumazet | cac5e65 | 2015-02-27 09:42:50 -0800 | [diff] [blame] | 1532 | hlist_for_each_entry(dev, head, index_hlist) { |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1533 | if (link_dump_filtered(dev, master_idx, kind_ops)) |
| 1534 | continue; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1535 | if (idx < s_idx) |
| 1536 | goto cont; |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1537 | err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
| 1538 | NETLINK_CB(cb->skb).portid, |
| 1539 | cb->nlh->nlmsg_seq, 0, |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1540 | flags, |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1541 | ext_filter_mask); |
| 1542 | /* If we ran out of room on the first message, |
| 1543 | * we're in trouble |
| 1544 | */ |
| 1545 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); |
| 1546 | |
David S. Miller | 7b46a64 | 2015-01-18 23:36:08 -0500 | [diff] [blame] | 1547 | if (err < 0) |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1548 | goto out; |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1549 | |
| 1550 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1551 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1552 | idx++; |
| 1553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1555 | out: |
| 1556 | cb->args[1] = idx; |
| 1557 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
| 1559 | return skb->len; |
| 1560 | } |
| 1561 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1562 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len) |
| 1563 | { |
| 1564 | return nla_parse(tb, IFLA_MAX, head, len, ifla_policy); |
| 1565 | } |
| 1566 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1567 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1568 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 1569 | { |
| 1570 | struct net *net; |
| 1571 | /* Examine the link attributes and figure out which |
| 1572 | * network namespace we are talking about. |
| 1573 | */ |
| 1574 | if (tb[IFLA_NET_NS_PID]) |
| 1575 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1576 | else if (tb[IFLA_NET_NS_FD]) |
| 1577 | net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1578 | else |
| 1579 | net = get_net(src_net); |
| 1580 | return net; |
| 1581 | } |
| 1582 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 1583 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1584 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
| 1585 | { |
| 1586 | if (dev) { |
| 1587 | if (tb[IFLA_ADDRESS] && |
| 1588 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 1589 | return -EINVAL; |
| 1590 | |
| 1591 | if (tb[IFLA_BROADCAST] && |
| 1592 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 1593 | return -EINVAL; |
| 1594 | } |
| 1595 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1596 | if (tb[IFLA_AF_SPEC]) { |
| 1597 | struct nlattr *af; |
| 1598 | int rem, err; |
| 1599 | |
| 1600 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 1601 | const struct rtnl_af_ops *af_ops; |
| 1602 | |
| 1603 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
| 1604 | return -EAFNOSUPPORT; |
| 1605 | |
| 1606 | if (!af_ops->set_link_af) |
| 1607 | return -EOPNOTSUPP; |
| 1608 | |
| 1609 | if (af_ops->validate_link_af) { |
Kurt Van Dijck | 6d3a9a6 | 2011-01-26 04:55:24 +0000 | [diff] [blame] | 1610 | err = af_ops->validate_link_af(dev, af); |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1611 | if (err < 0) |
| 1612 | return err; |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
| 1619 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1620 | static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, |
| 1621 | int guid_type) |
| 1622 | { |
| 1623 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1624 | |
| 1625 | return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); |
| 1626 | } |
| 1627 | |
| 1628 | static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) |
| 1629 | { |
| 1630 | if (dev->type != ARPHRD_INFINIBAND) |
| 1631 | return -EOPNOTSUPP; |
| 1632 | |
| 1633 | return handle_infiniband_guid(dev, ivt, guid_type); |
| 1634 | } |
| 1635 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1636 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1637 | { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1638 | const struct net_device_ops *ops = dev->netdev_ops; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1639 | int err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1640 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1641 | if (tb[IFLA_VF_MAC]) { |
| 1642 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1643 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1644 | err = -EOPNOTSUPP; |
| 1645 | if (ops->ndo_set_vf_mac) |
| 1646 | err = ops->ndo_set_vf_mac(dev, ivm->vf, |
| 1647 | ivm->mac); |
| 1648 | if (err < 0) |
| 1649 | return err; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1650 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1651 | |
| 1652 | if (tb[IFLA_VF_VLAN]) { |
| 1653 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); |
| 1654 | |
| 1655 | err = -EOPNOTSUPP; |
| 1656 | if (ops->ndo_set_vf_vlan) |
| 1657 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, |
| 1658 | ivv->qos); |
| 1659 | if (err < 0) |
| 1660 | return err; |
| 1661 | } |
| 1662 | |
| 1663 | if (tb[IFLA_VF_TX_RATE]) { |
| 1664 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); |
| 1665 | struct ifla_vf_info ivf; |
| 1666 | |
| 1667 | err = -EOPNOTSUPP; |
| 1668 | if (ops->ndo_get_vf_config) |
| 1669 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); |
| 1670 | if (err < 0) |
| 1671 | return err; |
| 1672 | |
| 1673 | err = -EOPNOTSUPP; |
| 1674 | if (ops->ndo_set_vf_rate) |
| 1675 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 1676 | ivf.min_tx_rate, |
| 1677 | ivt->rate); |
| 1678 | if (err < 0) |
| 1679 | return err; |
| 1680 | } |
| 1681 | |
| 1682 | if (tb[IFLA_VF_RATE]) { |
| 1683 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); |
| 1684 | |
| 1685 | err = -EOPNOTSUPP; |
| 1686 | if (ops->ndo_set_vf_rate) |
| 1687 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 1688 | ivt->min_tx_rate, |
| 1689 | ivt->max_tx_rate); |
| 1690 | if (err < 0) |
| 1691 | return err; |
| 1692 | } |
| 1693 | |
| 1694 | if (tb[IFLA_VF_SPOOFCHK]) { |
| 1695 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); |
| 1696 | |
| 1697 | err = -EOPNOTSUPP; |
| 1698 | if (ops->ndo_set_vf_spoofchk) |
| 1699 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, |
| 1700 | ivs->setting); |
| 1701 | if (err < 0) |
| 1702 | return err; |
| 1703 | } |
| 1704 | |
| 1705 | if (tb[IFLA_VF_LINK_STATE]) { |
| 1706 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); |
| 1707 | |
| 1708 | err = -EOPNOTSUPP; |
| 1709 | if (ops->ndo_set_vf_link_state) |
| 1710 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, |
| 1711 | ivl->link_state); |
| 1712 | if (err < 0) |
| 1713 | return err; |
| 1714 | } |
| 1715 | |
| 1716 | if (tb[IFLA_VF_RSS_QUERY_EN]) { |
| 1717 | struct ifla_vf_rss_query_en *ivrssq_en; |
| 1718 | |
| 1719 | err = -EOPNOTSUPP; |
| 1720 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); |
| 1721 | if (ops->ndo_set_vf_rss_query_en) |
| 1722 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, |
| 1723 | ivrssq_en->setting); |
| 1724 | if (err < 0) |
| 1725 | return err; |
| 1726 | } |
| 1727 | |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1728 | if (tb[IFLA_VF_TRUST]) { |
| 1729 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); |
| 1730 | |
| 1731 | err = -EOPNOTSUPP; |
| 1732 | if (ops->ndo_set_vf_trust) |
| 1733 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); |
| 1734 | if (err < 0) |
| 1735 | return err; |
| 1736 | } |
| 1737 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1738 | if (tb[IFLA_VF_IB_NODE_GUID]) { |
| 1739 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); |
| 1740 | |
| 1741 | if (!ops->ndo_set_vf_guid) |
| 1742 | return -EOPNOTSUPP; |
| 1743 | |
| 1744 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); |
| 1745 | } |
| 1746 | |
| 1747 | if (tb[IFLA_VF_IB_PORT_GUID]) { |
| 1748 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); |
| 1749 | |
| 1750 | if (!ops->ndo_set_vf_guid) |
| 1751 | return -EOPNOTSUPP; |
| 1752 | |
| 1753 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); |
| 1754 | } |
| 1755 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1756 | return err; |
| 1757 | } |
| 1758 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1759 | static int do_set_master(struct net_device *dev, int ifindex) |
| 1760 | { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1761 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1762 | const struct net_device_ops *ops; |
| 1763 | int err; |
| 1764 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1765 | if (upper_dev) { |
| 1766 | if (upper_dev->ifindex == ifindex) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1767 | return 0; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1768 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1769 | if (ops->ndo_del_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1770 | err = ops->ndo_del_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1771 | if (err) |
| 1772 | return err; |
| 1773 | } else { |
| 1774 | return -EOPNOTSUPP; |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | if (ifindex) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1779 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
| 1780 | if (!upper_dev) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1781 | return -EINVAL; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1782 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1783 | if (ops->ndo_add_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1784 | err = ops->ndo_add_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1785 | if (err) |
| 1786 | return err; |
| 1787 | } else { |
| 1788 | return -EOPNOTSUPP; |
| 1789 | } |
| 1790 | } |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1794 | #define DO_SETLINK_MODIFIED 0x01 |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1795 | /* notify flag means notify + modified. */ |
| 1796 | #define DO_SETLINK_NOTIFY 0x03 |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1797 | static int do_setlink(const struct sk_buff *skb, |
| 1798 | struct net_device *dev, struct ifinfomsg *ifm, |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1799 | struct nlattr **tb, char *ifname, int status) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1800 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1801 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1802 | int err; |
| 1803 | |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1804 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1805 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1806 | if (IS_ERR(net)) { |
| 1807 | err = PTR_ERR(net); |
| 1808 | goto errout; |
| 1809 | } |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1810 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
Nicolas Dichtel | e0ebde0 | 2014-11-27 10:16:15 +0100 | [diff] [blame] | 1811 | put_net(net); |
Eric W. Biederman | b51642f | 2012-11-16 03:03:11 +0000 | [diff] [blame] | 1812 | err = -EPERM; |
| 1813 | goto errout; |
| 1814 | } |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1815 | err = dev_change_net_namespace(dev, net, ifname); |
| 1816 | put_net(net); |
| 1817 | if (err) |
| 1818 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1819 | status |= DO_SETLINK_MODIFIED; |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1820 | } |
| 1821 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1822 | if (tb[IFLA_MAP]) { |
| 1823 | struct rtnl_link_ifmap *u_map; |
| 1824 | struct ifmap k_map; |
| 1825 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1826 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1827 | err = -EOPNOTSUPP; |
| 1828 | goto errout; |
| 1829 | } |
| 1830 | |
| 1831 | if (!netif_device_present(dev)) { |
| 1832 | err = -ENODEV; |
| 1833 | goto errout; |
| 1834 | } |
| 1835 | |
| 1836 | u_map = nla_data(tb[IFLA_MAP]); |
| 1837 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 1838 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 1839 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 1840 | k_map.irq = (unsigned char) u_map->irq; |
| 1841 | k_map.dma = (unsigned char) u_map->dma; |
| 1842 | k_map.port = (unsigned char) u_map->port; |
| 1843 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1844 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1845 | if (err < 0) |
| 1846 | goto errout; |
| 1847 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1848 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | if (tb[IFLA_ADDRESS]) { |
| 1852 | struct sockaddr *sa; |
| 1853 | int len; |
| 1854 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1855 | len = sizeof(sa_family_t) + dev->addr_len; |
| 1856 | sa = kmalloc(len, GFP_KERNEL); |
| 1857 | if (!sa) { |
| 1858 | err = -ENOMEM; |
| 1859 | goto errout; |
| 1860 | } |
| 1861 | sa->sa_family = dev->type; |
| 1862 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 1863 | dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1864 | err = dev_set_mac_address(dev, sa); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1865 | kfree(sa); |
| 1866 | if (err) |
| 1867 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1868 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | if (tb[IFLA_MTU]) { |
| 1872 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1873 | if (err < 0) |
| 1874 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1875 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1878 | if (tb[IFLA_GROUP]) { |
| 1879 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1880 | status |= DO_SETLINK_NOTIFY; |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1883 | /* |
| 1884 | * Interface selected by interface index but interface |
| 1885 | * name provided implies that a name change has been |
| 1886 | * requested. |
| 1887 | */ |
| 1888 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 1889 | err = dev_change_name(dev, ifname); |
| 1890 | if (err < 0) |
| 1891 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1892 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1895 | if (tb[IFLA_IFALIAS]) { |
| 1896 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 1897 | nla_len(tb[IFLA_IFALIAS])); |
| 1898 | if (err < 0) |
| 1899 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1900 | status |= DO_SETLINK_NOTIFY; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1903 | if (tb[IFLA_BROADCAST]) { |
| 1904 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1905 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | if (ifm->ifi_flags || ifm->ifi_change) { |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1909 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 1910 | if (err < 0) |
| 1911 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1914 | if (tb[IFLA_MASTER]) { |
| 1915 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); |
| 1916 | if (err) |
| 1917 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1918 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1921 | if (tb[IFLA_CARRIER]) { |
| 1922 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); |
| 1923 | if (err) |
| 1924 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1925 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1928 | if (tb[IFLA_TXQLEN]) { |
| 1929 | unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1930 | |
| 1931 | if (dev->tx_queue_len ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1932 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1933 | |
| 1934 | dev->tx_queue_len = value; |
| 1935 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1936 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1937 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1938 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1939 | |
| 1940 | if (tb[IFLA_LINKMODE]) { |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1941 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
| 1942 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1943 | write_lock_bh(&dev_base_lock); |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1944 | if (dev->link_mode ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1945 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1946 | dev->link_mode = value; |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1947 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1950 | if (tb[IFLA_VFINFO_LIST]) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1951 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1952 | struct nlattr *attr; |
| 1953 | int rem; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1954 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1955 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1956 | if (nla_type(attr) != IFLA_VF_INFO || |
| 1957 | nla_len(attr) < NLA_HDRLEN) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1958 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1959 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1960 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1961 | err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, |
| 1962 | ifla_vf_policy); |
| 1963 | if (err < 0) |
| 1964 | goto errout; |
| 1965 | err = do_setvfinfo(dev, vfinfo); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1966 | if (err < 0) |
| 1967 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1968 | status |= DO_SETLINK_NOTIFY; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1969 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1970 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1971 | err = 0; |
| 1972 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1973 | if (tb[IFLA_VF_PORTS]) { |
| 1974 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1975 | struct nlattr *attr; |
| 1976 | int vf; |
| 1977 | int rem; |
| 1978 | |
| 1979 | err = -EOPNOTSUPP; |
| 1980 | if (!ops->ndo_set_vf_port) |
| 1981 | goto errout; |
| 1982 | |
| 1983 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
Daniel Borkmann | 035d210 | 2015-07-13 00:06:02 +0200 | [diff] [blame] | 1984 | if (nla_type(attr) != IFLA_VF_PORT || |
| 1985 | nla_len(attr) < NLA_HDRLEN) { |
| 1986 | err = -EINVAL; |
| 1987 | goto errout; |
| 1988 | } |
| 1989 | err = nla_parse_nested(port, IFLA_PORT_MAX, attr, |
| 1990 | ifla_port_policy); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1991 | if (err < 0) |
| 1992 | goto errout; |
| 1993 | if (!port[IFLA_PORT_VF]) { |
| 1994 | err = -EOPNOTSUPP; |
| 1995 | goto errout; |
| 1996 | } |
| 1997 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 1998 | err = ops->ndo_set_vf_port(dev, vf, port); |
| 1999 | if (err < 0) |
| 2000 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2001 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | err = 0; |
| 2005 | |
| 2006 | if (tb[IFLA_PORT_SELF]) { |
| 2007 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 2008 | |
| 2009 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 2010 | tb[IFLA_PORT_SELF], ifla_port_policy); |
| 2011 | if (err < 0) |
| 2012 | goto errout; |
| 2013 | |
| 2014 | err = -EOPNOTSUPP; |
| 2015 | if (ops->ndo_set_vf_port) |
| 2016 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 2017 | if (err < 0) |
| 2018 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2019 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2020 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2021 | |
| 2022 | if (tb[IFLA_AF_SPEC]) { |
| 2023 | struct nlattr *af; |
| 2024 | int rem; |
| 2025 | |
| 2026 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2027 | const struct rtnl_af_ops *af_ops; |
| 2028 | |
| 2029 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2030 | BUG(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2031 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2032 | err = af_ops->set_link_af(dev, af); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2033 | if (err < 0) |
| 2034 | goto errout; |
| 2035 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2036 | status |= DO_SETLINK_NOTIFY; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2039 | err = 0; |
| 2040 | |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 2041 | if (tb[IFLA_PROTO_DOWN]) { |
| 2042 | err = dev_change_proto_down(dev, |
| 2043 | nla_get_u8(tb[IFLA_PROTO_DOWN])); |
| 2044 | if (err) |
| 2045 | goto errout; |
| 2046 | status |= DO_SETLINK_NOTIFY; |
| 2047 | } |
| 2048 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2049 | errout: |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2050 | if (status & DO_SETLINK_MODIFIED) { |
| 2051 | if (status & DO_SETLINK_NOTIFY) |
| 2052 | netdev_state_change(dev); |
| 2053 | |
| 2054 | if (err < 0) |
| 2055 | net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n", |
| 2056 | dev->name); |
| 2057 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2058 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2059 | return err; |
| 2060 | } |
| 2061 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2062 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2064 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2065 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2067 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2068 | struct nlattr *tb[IFLA_MAX+1]; |
| 2069 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2071 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2072 | if (err < 0) |
| 2073 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 2075 | if (tb[IFLA_IFNAME]) |
| 2076 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 2077 | else |
| 2078 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2081 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 2082 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2083 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2084 | else if (tb[IFLA_IFNAME]) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2085 | dev = __dev_get_by_name(net, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2086 | else |
| 2087 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2089 | if (dev == NULL) { |
| 2090 | err = -ENODEV; |
| 2091 | goto errout; |
| 2092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2094 | err = validate_linkmsg(dev, tb); |
| 2095 | if (err < 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2096 | goto errout; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2097 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2098 | err = do_setlink(skb, dev, ifm, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2099 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | return err; |
| 2101 | } |
| 2102 | |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 2103 | static int rtnl_group_dellink(const struct net *net, int group) |
| 2104 | { |
| 2105 | struct net_device *dev, *aux; |
| 2106 | LIST_HEAD(list_kill); |
| 2107 | bool found = false; |
| 2108 | |
| 2109 | if (!group) |
| 2110 | return -EPERM; |
| 2111 | |
| 2112 | for_each_netdev(net, dev) { |
| 2113 | if (dev->group == group) { |
| 2114 | const struct rtnl_link_ops *ops; |
| 2115 | |
| 2116 | found = true; |
| 2117 | ops = dev->rtnl_link_ops; |
| 2118 | if (!ops || !ops->dellink) |
| 2119 | return -EOPNOTSUPP; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | if (!found) |
| 2124 | return -ENODEV; |
| 2125 | |
| 2126 | for_each_netdev_safe(net, dev, aux) { |
| 2127 | if (dev->group == group) { |
| 2128 | const struct rtnl_link_ops *ops; |
| 2129 | |
| 2130 | ops = dev->rtnl_link_ops; |
| 2131 | ops->dellink(dev, &list_kill); |
| 2132 | } |
| 2133 | } |
| 2134 | unregister_netdevice_many(&list_kill); |
| 2135 | |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 2139 | int rtnl_delete_link(struct net_device *dev) |
| 2140 | { |
| 2141 | const struct rtnl_link_ops *ops; |
| 2142 | LIST_HEAD(list_kill); |
| 2143 | |
| 2144 | ops = dev->rtnl_link_ops; |
| 2145 | if (!ops || !ops->dellink) |
| 2146 | return -EOPNOTSUPP; |
| 2147 | |
| 2148 | ops->dellink(dev, &list_kill); |
| 2149 | unregister_netdevice_many(&list_kill); |
| 2150 | |
| 2151 | return 0; |
| 2152 | } |
| 2153 | EXPORT_SYMBOL_GPL(rtnl_delete_link); |
| 2154 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2155 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2156 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2157 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2158 | struct net_device *dev; |
| 2159 | struct ifinfomsg *ifm; |
| 2160 | char ifname[IFNAMSIZ]; |
| 2161 | struct nlattr *tb[IFLA_MAX+1]; |
| 2162 | int err; |
| 2163 | |
| 2164 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2165 | if (err < 0) |
| 2166 | return err; |
| 2167 | |
| 2168 | if (tb[IFLA_IFNAME]) |
| 2169 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2170 | |
| 2171 | ifm = nlmsg_data(nlh); |
| 2172 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2173 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2174 | else if (tb[IFLA_IFNAME]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2175 | dev = __dev_get_by_name(net, ifname); |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 2176 | else if (tb[IFLA_GROUP]) |
| 2177 | return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2178 | else |
| 2179 | return -EINVAL; |
| 2180 | |
| 2181 | if (!dev) |
| 2182 | return -ENODEV; |
| 2183 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 2184 | return rtnl_delete_link(dev); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2187 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 2188 | { |
| 2189 | unsigned int old_flags; |
| 2190 | int err; |
| 2191 | |
| 2192 | old_flags = dev->flags; |
| 2193 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
| 2194 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
| 2195 | if (err < 0) |
| 2196 | return err; |
| 2197 | } |
| 2198 | |
| 2199 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2200 | |
Nicolas Dichtel | a528c21 | 2013-09-25 12:02:44 +0200 | [diff] [blame] | 2201 | __dev_notify_flags(dev, old_flags, ~0U); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2202 | return 0; |
| 2203 | } |
| 2204 | EXPORT_SYMBOL(rtnl_configure_link); |
| 2205 | |
Rami Rosen | c071356 | 2012-11-30 01:08:47 +0000 | [diff] [blame] | 2206 | struct net_device *rtnl_create_link(struct net *net, |
Thomas Graf | 78ebb0d | 2015-04-10 01:45:53 +0200 | [diff] [blame] | 2207 | const char *ifname, unsigned char name_assign_type, |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2208 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2209 | { |
| 2210 | int err; |
| 2211 | struct net_device *dev; |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2212 | unsigned int num_tx_queues = 1; |
| 2213 | unsigned int num_rx_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2214 | |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 2215 | if (tb[IFLA_NUM_TX_QUEUES]) |
| 2216 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); |
| 2217 | else if (ops->get_num_tx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2218 | num_tx_queues = ops->get_num_tx_queues(); |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (tb[IFLA_NUM_RX_QUEUES]) |
| 2221 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); |
| 2222 | else if (ops->get_num_rx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2223 | num_rx_queues = ops->get_num_rx_queues(); |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 2224 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2225 | err = -ENOMEM; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2226 | dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 2227 | ops->setup, num_tx_queues, num_rx_queues); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2228 | if (!dev) |
| 2229 | goto err; |
| 2230 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2231 | dev_net_set(dev, net); |
| 2232 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2233 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2234 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2235 | if (tb[IFLA_MTU]) |
| 2236 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 2237 | if (tb[IFLA_ADDRESS]) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2238 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 2239 | nla_len(tb[IFLA_ADDRESS])); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 2240 | dev->addr_assign_type = NET_ADDR_SET; |
| 2241 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2242 | if (tb[IFLA_BROADCAST]) |
| 2243 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 2244 | nla_len(tb[IFLA_BROADCAST])); |
| 2245 | if (tb[IFLA_TXQLEN]) |
| 2246 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 2247 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2248 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2249 | if (tb[IFLA_LINKMODE]) |
| 2250 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2251 | if (tb[IFLA_GROUP]) |
| 2252 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2253 | |
| 2254 | return dev; |
| 2255 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2256 | err: |
| 2257 | return ERR_PTR(err); |
| 2258 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2259 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2260 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2261 | static int rtnl_group_changelink(const struct sk_buff *skb, |
| 2262 | struct net *net, int group, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2263 | struct ifinfomsg *ifm, |
| 2264 | struct nlattr **tb) |
| 2265 | { |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 2266 | struct net_device *dev, *aux; |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2267 | int err; |
| 2268 | |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 2269 | for_each_netdev_safe(net, dev, aux) { |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2270 | if (dev->group == group) { |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2271 | err = do_setlink(skb, dev, ifm, tb, NULL, 0); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2272 | if (err < 0) |
| 2273 | return err; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2280 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2281 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2282 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2283 | const struct rtnl_link_ops *ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2284 | const struct rtnl_link_ops *m_ops = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2285 | struct net_device *dev; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2286 | struct net_device *master_dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2287 | struct ifinfomsg *ifm; |
| 2288 | char kind[MODULE_NAME_LEN]; |
| 2289 | char ifname[IFNAMSIZ]; |
| 2290 | struct nlattr *tb[IFLA_MAX+1]; |
| 2291 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2292 | unsigned char name_assign_type = NET_NAME_USER; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2293 | int err; |
| 2294 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2295 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2296 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 2297 | #endif |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2298 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2299 | if (err < 0) |
| 2300 | return err; |
| 2301 | |
| 2302 | if (tb[IFLA_IFNAME]) |
| 2303 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2304 | else |
| 2305 | ifname[0] = '\0'; |
| 2306 | |
| 2307 | ifm = nlmsg_data(nlh); |
| 2308 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2309 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2310 | else { |
| 2311 | if (ifname[0]) |
| 2312 | dev = __dev_get_by_name(net, ifname); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2313 | else |
| 2314 | dev = NULL; |
| 2315 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2316 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2317 | if (dev) { |
| 2318 | master_dev = netdev_master_upper_dev_get(dev); |
| 2319 | if (master_dev) |
| 2320 | m_ops = master_dev->rtnl_link_ops; |
| 2321 | } |
| 2322 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2323 | err = validate_linkmsg(dev, tb); |
| 2324 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2325 | return err; |
| 2326 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2327 | if (tb[IFLA_LINKINFO]) { |
| 2328 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, |
| 2329 | tb[IFLA_LINKINFO], ifla_info_policy); |
| 2330 | if (err < 0) |
| 2331 | return err; |
| 2332 | } else |
| 2333 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 2334 | |
| 2335 | if (linkinfo[IFLA_INFO_KIND]) { |
| 2336 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
| 2337 | ops = rtnl_link_ops_get(kind); |
| 2338 | } else { |
| 2339 | kind[0] = '\0'; |
| 2340 | ops = NULL; |
| 2341 | } |
| 2342 | |
| 2343 | if (1) { |
Sasha Levin | 4e10fd5 | 2015-02-24 14:14:35 -0500 | [diff] [blame] | 2344 | struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; |
| 2345 | struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2346 | struct nlattr **data = NULL; |
| 2347 | struct nlattr **slave_data = NULL; |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2348 | struct net *dest_net, *link_net = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2349 | |
| 2350 | if (ops) { |
| 2351 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
| 2352 | err = nla_parse_nested(attr, ops->maxtype, |
| 2353 | linkinfo[IFLA_INFO_DATA], |
| 2354 | ops->policy); |
| 2355 | if (err < 0) |
| 2356 | return err; |
| 2357 | data = attr; |
| 2358 | } |
| 2359 | if (ops->validate) { |
| 2360 | err = ops->validate(tb, data); |
| 2361 | if (err < 0) |
| 2362 | return err; |
| 2363 | } |
| 2364 | } |
| 2365 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2366 | if (m_ops) { |
| 2367 | if (m_ops->slave_maxtype && |
| 2368 | linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2369 | err = nla_parse_nested(slave_attr, |
| 2370 | m_ops->slave_maxtype, |
| 2371 | linkinfo[IFLA_INFO_SLAVE_DATA], |
| 2372 | m_ops->slave_policy); |
| 2373 | if (err < 0) |
| 2374 | return err; |
| 2375 | slave_data = slave_attr; |
| 2376 | } |
| 2377 | if (m_ops->slave_validate) { |
| 2378 | err = m_ops->slave_validate(tb, slave_data); |
| 2379 | if (err < 0) |
| 2380 | return err; |
| 2381 | } |
| 2382 | } |
| 2383 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2384 | if (dev) { |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2385 | int status = 0; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2386 | |
| 2387 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 2388 | return -EEXIST; |
| 2389 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2390 | return -EOPNOTSUPP; |
| 2391 | |
| 2392 | if (linkinfo[IFLA_INFO_DATA]) { |
| 2393 | if (!ops || ops != dev->rtnl_link_ops || |
| 2394 | !ops->changelink) |
| 2395 | return -EOPNOTSUPP; |
| 2396 | |
| 2397 | err = ops->changelink(dev, tb, data); |
| 2398 | if (err < 0) |
| 2399 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2400 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2401 | } |
| 2402 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2403 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2404 | if (!m_ops || !m_ops->slave_changelink) |
| 2405 | return -EOPNOTSUPP; |
| 2406 | |
| 2407 | err = m_ops->slave_changelink(master_dev, dev, |
| 2408 | tb, slave_data); |
| 2409 | if (err < 0) |
| 2410 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2411 | status |= DO_SETLINK_NOTIFY; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2412 | } |
| 2413 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2414 | return do_setlink(skb, dev, ifm, tb, ifname, status); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2417 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 2418 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2419 | return rtnl_group_changelink(skb, net, |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2420 | nla_get_u32(tb[IFLA_GROUP]), |
| 2421 | ifm, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2422 | return -ENODEV; |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2423 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2424 | |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 2425 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2426 | return -EOPNOTSUPP; |
| 2427 | |
| 2428 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2429 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2430 | if (kind[0]) { |
| 2431 | __rtnl_unlock(); |
| 2432 | request_module("rtnl-link-%s", kind); |
| 2433 | rtnl_lock(); |
| 2434 | ops = rtnl_link_ops_get(kind); |
| 2435 | if (ops) |
| 2436 | goto replay; |
| 2437 | } |
| 2438 | #endif |
| 2439 | return -EOPNOTSUPP; |
| 2440 | } |
| 2441 | |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 2442 | if (!ops->setup) |
| 2443 | return -EOPNOTSUPP; |
| 2444 | |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2445 | if (!ifname[0]) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2446 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2447 | name_assign_type = NET_NAME_ENUM; |
| 2448 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2449 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2450 | dest_net = rtnl_link_get_net(net, tb); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 2451 | if (IS_ERR(dest_net)) |
| 2452 | return PTR_ERR(dest_net); |
| 2453 | |
Eric W. Biederman | 505ce41 | 2015-02-26 16:19:00 -0600 | [diff] [blame] | 2454 | err = -EPERM; |
| 2455 | if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) |
| 2456 | goto out; |
| 2457 | |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2458 | if (tb[IFLA_LINK_NETNSID]) { |
| 2459 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); |
| 2460 | |
| 2461 | link_net = get_net_ns_by_id(dest_net, id); |
| 2462 | if (!link_net) { |
| 2463 | err = -EINVAL; |
| 2464 | goto out; |
| 2465 | } |
Eric W. Biederman | 06615be | 2015-02-26 16:20:07 -0600 | [diff] [blame] | 2466 | err = -EPERM; |
| 2467 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) |
| 2468 | goto out; |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | dev = rtnl_create_link(link_net ? : dest_net, ifname, |
| 2472 | name_assign_type, ops, tb); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2473 | if (IS_ERR(dev)) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2474 | err = PTR_ERR(dev); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2475 | goto out; |
| 2476 | } |
| 2477 | |
| 2478 | dev->ifindex = ifm->ifi_index; |
| 2479 | |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2480 | if (ops->newlink) { |
Nicolas Dichtel | 7b4ce69 | 2015-01-27 11:13:08 +0100 | [diff] [blame] | 2481 | err = ops->newlink(link_net ? : net, dev, tb, data); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2482 | /* Drivers should call free_netdev() in ->destructor |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2483 | * and unregister it on failure after registration |
| 2484 | * so that device could be finally freed in rtnl_unlock. |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2485 | */ |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2486 | if (err < 0) { |
| 2487 | /* If device is not registered at all, free it now */ |
| 2488 | if (dev->reg_state == NETREG_UNINITIALIZED) |
| 2489 | free_netdev(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2490 | goto out; |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2491 | } |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2492 | } else { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 2493 | err = register_netdevice(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2494 | if (err < 0) { |
| 2495 | free_netdev(dev); |
| 2496 | goto out; |
| 2497 | } |
Dan Carpenter | fce9b9b | 2013-08-14 12:35:42 +0300 | [diff] [blame] | 2498 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2499 | err = rtnl_configure_link(dev, ifm); |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2500 | if (err < 0) |
| 2501 | goto out_unregister; |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2502 | if (link_net) { |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2503 | err = dev_change_net_namespace(dev, dest_net, ifname); |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2504 | if (err < 0) |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2505 | goto out_unregister; |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2506 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2507 | out: |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2508 | if (link_net) |
| 2509 | put_net(link_net); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2510 | put_net(dest_net); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2511 | return err; |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2512 | out_unregister: |
| 2513 | if (ops->newlink) { |
| 2514 | LIST_HEAD(list_kill); |
| 2515 | |
| 2516 | ops->dellink(dev, &list_kill); |
| 2517 | unregister_netdevice_many(&list_kill); |
| 2518 | } else { |
| 2519 | unregister_netdevice(dev); |
| 2520 | } |
| 2521 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2522 | } |
| 2523 | } |
| 2524 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2525 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2526 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2527 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2528 | struct ifinfomsg *ifm; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2529 | char ifname[IFNAMSIZ]; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2530 | struct nlattr *tb[IFLA_MAX+1]; |
| 2531 | struct net_device *dev = NULL; |
| 2532 | struct sk_buff *nskb; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2533 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2534 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2535 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2536 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2537 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 2538 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2539 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2540 | if (tb[IFLA_IFNAME]) |
| 2541 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2542 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2543 | if (tb[IFLA_EXT_MASK]) |
| 2544 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2545 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2546 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2547 | if (ifm->ifi_index > 0) |
| 2548 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 2549 | else if (tb[IFLA_IFNAME]) |
| 2550 | dev = __dev_get_by_name(net, ifname); |
| 2551 | else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2552 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2553 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2554 | if (dev == NULL) |
| 2555 | return -ENODEV; |
| 2556 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2557 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2558 | if (nskb == NULL) |
| 2559 | return -ENOBUFS; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2560 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2561 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2562 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2563 | if (err < 0) { |
| 2564 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 2565 | WARN_ON(err == -EMSGSIZE); |
| 2566 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2567 | } else |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2568 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2569 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2570 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2571 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2572 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2573 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2574 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2575 | struct net *net = sock_net(skb->sk); |
| 2576 | struct net_device *dev; |
| 2577 | struct nlattr *tb[IFLA_MAX+1]; |
| 2578 | u32 ext_filter_mask = 0; |
| 2579 | u16 min_ifinfo_dump_size = 0; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2580 | int hdrlen; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2581 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2582 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ |
| 2583 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 2584 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 2585 | |
| 2586 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 2587 | if (tb[IFLA_EXT_MASK]) |
| 2588 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2589 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2590 | |
| 2591 | if (!ext_filter_mask) |
| 2592 | return NLMSG_GOODSIZE; |
| 2593 | /* |
| 2594 | * traverse the list of net devices and compute the minimum |
| 2595 | * buffer size based upon the filter mask. |
| 2596 | */ |
| 2597 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { |
| 2598 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, |
| 2599 | if_nlmsg_size(dev, |
| 2600 | ext_filter_mask)); |
| 2601 | } |
| 2602 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2603 | return min_ifinfo_dump_size; |
| 2604 | } |
| 2605 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 2606 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | { |
| 2608 | int idx; |
| 2609 | int s_idx = cb->family; |
| 2610 | |
| 2611 | if (s_idx == 0) |
| 2612 | s_idx = 1; |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 2613 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 2615 | if (idx < s_idx || idx == PF_PACKET) |
| 2616 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2617 | if (rtnl_msg_handlers[idx] == NULL || |
| 2618 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | continue; |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2620 | if (idx > s_idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2622 | cb->prev_seq = 0; |
| 2623 | cb->seq = 0; |
| 2624 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2625 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | break; |
| 2627 | } |
| 2628 | cb->family = idx; |
| 2629 | |
| 2630 | return skb->len; |
| 2631 | } |
| 2632 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2633 | struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, |
| 2634 | unsigned int change, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2636 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2638 | int err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2639 | size_t if_info_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 2641 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2642 | if (skb == NULL) |
| 2643 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2645 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2646 | if (err < 0) { |
| 2647 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 2648 | WARN_ON(err == -EMSGSIZE); |
| 2649 | kfree_skb(skb); |
| 2650 | goto errout; |
| 2651 | } |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2652 | return skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2653 | errout: |
| 2654 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 2655 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2656 | return NULL; |
| 2657 | } |
| 2658 | |
| 2659 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) |
| 2660 | { |
| 2661 | struct net *net = dev_net(dev); |
| 2662 | |
| 2663 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); |
| 2664 | } |
| 2665 | |
| 2666 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, |
| 2667 | gfp_t flags) |
| 2668 | { |
| 2669 | struct sk_buff *skb; |
| 2670 | |
Nicolas Dichtel | ed2a80a | 2015-05-13 14:19:42 +0200 | [diff] [blame] | 2671 | if (dev->reg_state != NETREG_REGISTERED) |
| 2672 | return; |
| 2673 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2674 | skb = rtmsg_ifinfo_build_skb(type, dev, change, flags); |
| 2675 | if (skb) |
| 2676 | rtmsg_ifinfo_send(skb, dev, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 2678 | EXPORT_SYMBOL(rtmsg_ifinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2680 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
| 2681 | struct net_device *dev, |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2682 | u8 *addr, u16 vid, u32 pid, u32 seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2683 | int type, unsigned int flags, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2684 | int nlflags, u16 ndm_state) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2685 | { |
| 2686 | struct nlmsghdr *nlh; |
| 2687 | struct ndmsg *ndm; |
| 2688 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2689 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2690 | if (!nlh) |
| 2691 | return -EMSGSIZE; |
| 2692 | |
| 2693 | ndm = nlmsg_data(nlh); |
| 2694 | ndm->ndm_family = AF_BRIDGE; |
| 2695 | ndm->ndm_pad1 = 0; |
| 2696 | ndm->ndm_pad2 = 0; |
| 2697 | ndm->ndm_flags = flags; |
| 2698 | ndm->ndm_type = 0; |
| 2699 | ndm->ndm_ifindex = dev->ifindex; |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2700 | ndm->ndm_state = ndm_state; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2701 | |
| 2702 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 2703 | goto nla_put_failure; |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2704 | if (vid) |
| 2705 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) |
| 2706 | goto nla_put_failure; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2707 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2708 | nlmsg_end(skb, nlh); |
| 2709 | return 0; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2710 | |
| 2711 | nla_put_failure: |
| 2712 | nlmsg_cancel(skb, nlh); |
| 2713 | return -EMSGSIZE; |
| 2714 | } |
| 2715 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2716 | static inline size_t rtnl_fdb_nlmsg_size(void) |
| 2717 | { |
| 2718 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); |
| 2719 | } |
| 2720 | |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2721 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, |
| 2722 | u16 ndm_state) |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2723 | { |
| 2724 | struct net *net = dev_net(dev); |
| 2725 | struct sk_buff *skb; |
| 2726 | int err = -ENOBUFS; |
| 2727 | |
| 2728 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); |
| 2729 | if (!skb) |
| 2730 | goto errout; |
| 2731 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2732 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2733 | 0, 0, type, NTF_SELF, 0, ndm_state); |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2734 | if (err < 0) { |
| 2735 | kfree_skb(skb); |
| 2736 | goto errout; |
| 2737 | } |
| 2738 | |
| 2739 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 2740 | return; |
| 2741 | errout: |
| 2742 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 2743 | } |
| 2744 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2745 | /** |
| 2746 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry |
| 2747 | */ |
| 2748 | int ndo_dflt_fdb_add(struct ndmsg *ndm, |
| 2749 | struct nlattr *tb[], |
| 2750 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2751 | const unsigned char *addr, u16 vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2752 | u16 flags) |
| 2753 | { |
| 2754 | int err = -EINVAL; |
| 2755 | |
| 2756 | /* If aging addresses are supported device will need to |
| 2757 | * implement its own handler for this. |
| 2758 | */ |
| 2759 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { |
| 2760 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
| 2761 | return err; |
| 2762 | } |
| 2763 | |
Or Gerlitz | 65891fe | 2014-12-14 18:19:05 +0200 | [diff] [blame] | 2764 | if (vid) { |
| 2765 | pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); |
| 2766 | return err; |
| 2767 | } |
| 2768 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2769 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2770 | err = dev_uc_add_excl(dev, addr); |
| 2771 | else if (is_multicast_ether_addr(addr)) |
| 2772 | err = dev_mc_add_excl(dev, addr); |
| 2773 | |
| 2774 | /* Only return duplicate errors if NLM_F_EXCL is set */ |
| 2775 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) |
| 2776 | err = 0; |
| 2777 | |
| 2778 | return err; |
| 2779 | } |
| 2780 | EXPORT_SYMBOL(ndo_dflt_fdb_add); |
| 2781 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2782 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) |
| 2783 | { |
| 2784 | u16 vid = 0; |
| 2785 | |
| 2786 | if (vlan_attr) { |
| 2787 | if (nla_len(vlan_attr) != sizeof(u16)) { |
| 2788 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); |
| 2789 | return -EINVAL; |
| 2790 | } |
| 2791 | |
| 2792 | vid = nla_get_u16(vlan_attr); |
| 2793 | |
| 2794 | if (!vid || vid >= VLAN_VID_MASK) { |
| 2795 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", |
| 2796 | vid); |
| 2797 | return -EINVAL; |
| 2798 | } |
| 2799 | } |
| 2800 | *p_vid = vid; |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2804 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2805 | { |
| 2806 | struct net *net = sock_net(skb->sk); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2807 | struct ndmsg *ndm; |
| 2808 | struct nlattr *tb[NDA_MAX+1]; |
| 2809 | struct net_device *dev; |
| 2810 | u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2811 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2812 | int err; |
| 2813 | |
| 2814 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2815 | if (err < 0) |
| 2816 | return err; |
| 2817 | |
| 2818 | ndm = nlmsg_data(nlh); |
| 2819 | if (ndm->ndm_ifindex == 0) { |
| 2820 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); |
| 2821 | return -EINVAL; |
| 2822 | } |
| 2823 | |
| 2824 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2825 | if (dev == NULL) { |
| 2826 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); |
| 2827 | return -ENODEV; |
| 2828 | } |
| 2829 | |
| 2830 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2831 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); |
| 2832 | return -EINVAL; |
| 2833 | } |
| 2834 | |
| 2835 | addr = nla_data(tb[NDA_LLADDR]); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2836 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2837 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2838 | if (err) |
| 2839 | return err; |
| 2840 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2841 | err = -EOPNOTSUPP; |
| 2842 | |
| 2843 | /* Support fdb on master device the net/bridge default case */ |
| 2844 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2845 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2846 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2847 | const struct net_device_ops *ops = br_dev->netdev_ops; |
| 2848 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2849 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
| 2850 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2851 | if (err) |
| 2852 | goto out; |
| 2853 | else |
| 2854 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2855 | } |
| 2856 | |
| 2857 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2858 | if ((ndm->ndm_flags & NTF_SELF)) { |
| 2859 | if (dev->netdev_ops->ndo_fdb_add) |
| 2860 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2861 | vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2862 | nlh->nlmsg_flags); |
| 2863 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2864 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2865 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2866 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2867 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2868 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, |
| 2869 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2870 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2871 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2872 | } |
| 2873 | out: |
| 2874 | return err; |
| 2875 | } |
| 2876 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2877 | /** |
| 2878 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry |
| 2879 | */ |
| 2880 | int ndo_dflt_fdb_del(struct ndmsg *ndm, |
| 2881 | struct nlattr *tb[], |
| 2882 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2883 | const unsigned char *addr, u16 vid) |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2884 | { |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2885 | int err = -EINVAL; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2886 | |
| 2887 | /* If aging addresses are supported device will need to |
| 2888 | * implement its own handler for this. |
| 2889 | */ |
Sridhar Samudrala | 6453599 | 2013-08-08 15:19:48 -0700 | [diff] [blame] | 2890 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2891 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2892 | return err; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2896 | err = dev_uc_del(dev, addr); |
| 2897 | else if (is_multicast_ether_addr(addr)) |
| 2898 | err = dev_mc_del(dev, addr); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2899 | |
| 2900 | return err; |
| 2901 | } |
| 2902 | EXPORT_SYMBOL(ndo_dflt_fdb_del); |
| 2903 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2904 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2905 | { |
| 2906 | struct net *net = sock_net(skb->sk); |
| 2907 | struct ndmsg *ndm; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2908 | struct nlattr *tb[NDA_MAX+1]; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2909 | struct net_device *dev; |
| 2910 | int err = -EINVAL; |
| 2911 | __u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2912 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2913 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2914 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2915 | return -EPERM; |
| 2916 | |
| 2917 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2918 | if (err < 0) |
| 2919 | return err; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2920 | |
| 2921 | ndm = nlmsg_data(nlh); |
| 2922 | if (ndm->ndm_ifindex == 0) { |
| 2923 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); |
| 2924 | return -EINVAL; |
| 2925 | } |
| 2926 | |
| 2927 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2928 | if (dev == NULL) { |
| 2929 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); |
| 2930 | return -ENODEV; |
| 2931 | } |
| 2932 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2933 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2934 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2935 | return -EINVAL; |
| 2936 | } |
| 2937 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2938 | addr = nla_data(tb[NDA_LLADDR]); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2939 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2940 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2941 | if (err) |
| 2942 | return err; |
| 2943 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2944 | err = -EOPNOTSUPP; |
| 2945 | |
| 2946 | /* Support fdb on master device the net/bridge default case */ |
| 2947 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2948 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2949 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2950 | const struct net_device_ops *ops = br_dev->netdev_ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2951 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2952 | if (ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2953 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2954 | |
| 2955 | if (err) |
| 2956 | goto out; |
| 2957 | else |
| 2958 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2959 | } |
| 2960 | |
| 2961 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2962 | if (ndm->ndm_flags & NTF_SELF) { |
| 2963 | if (dev->netdev_ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2964 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
| 2965 | vid); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2966 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2967 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2968 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2969 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2970 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, |
| 2971 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2972 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2973 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2974 | } |
| 2975 | out: |
| 2976 | return err; |
| 2977 | } |
| 2978 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2979 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
| 2980 | struct netlink_callback *cb, |
| 2981 | struct net_device *dev, |
| 2982 | int *idx, |
| 2983 | struct netdev_hw_addr_list *list) |
| 2984 | { |
| 2985 | struct netdev_hw_addr *ha; |
| 2986 | int err; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2987 | u32 portid, seq; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2988 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2989 | portid = NETLINK_CB(cb->skb).portid; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2990 | seq = cb->nlh->nlmsg_seq; |
| 2991 | |
| 2992 | list_for_each_entry(ha, &list->list, list) { |
| 2993 | if (*idx < cb->args[0]) |
| 2994 | goto skip; |
| 2995 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2996 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
John Fastabend | a7a558f | 2012-11-01 16:23:10 +0000 | [diff] [blame] | 2997 | portid, seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2998 | RTM_NEWNEIGH, NTF_SELF, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2999 | NLM_F_MULTI, NUD_PERMANENT); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3000 | if (err < 0) |
| 3001 | return err; |
| 3002 | skip: |
| 3003 | *idx += 1; |
| 3004 | } |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
| 3008 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 3009 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3010 | * @nlh: netlink message header |
| 3011 | * @dev: netdevice |
| 3012 | * |
| 3013 | * Default netdevice operation to dump the existing unicast address list. |
John Fastabend | 91f3e7b | 2013-03-29 08:18:37 +0000 | [diff] [blame] | 3014 | * Returns number of addresses from list put in skb. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3015 | */ |
| 3016 | int ndo_dflt_fdb_dump(struct sk_buff *skb, |
| 3017 | struct netlink_callback *cb, |
| 3018 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 3019 | struct net_device *filter_dev, |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3020 | int idx) |
| 3021 | { |
| 3022 | int err; |
| 3023 | |
| 3024 | netif_addr_lock_bh(dev); |
| 3025 | err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); |
| 3026 | if (err) |
| 3027 | goto out; |
| 3028 | nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); |
| 3029 | out: |
| 3030 | netif_addr_unlock_bh(dev); |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3031 | cb->args[1] = err; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3032 | return idx; |
| 3033 | } |
| 3034 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); |
| 3035 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3036 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 3037 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3038 | struct net_device *dev; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3039 | struct nlattr *tb[IFLA_MAX+1]; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3040 | struct net_device *br_dev = NULL; |
| 3041 | const struct net_device_ops *ops = NULL; |
| 3042 | const struct net_device_ops *cops = NULL; |
| 3043 | struct ifinfomsg *ifm = nlmsg_data(cb->nlh); |
| 3044 | struct net *net = sock_net(skb->sk); |
| 3045 | int brport_idx = 0; |
| 3046 | int br_idx = 0; |
| 3047 | int idx = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3048 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3049 | if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, |
| 3050 | ifla_policy) == 0) { |
| 3051 | if (tb[IFLA_MASTER]) |
| 3052 | br_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 3053 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3054 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3055 | brport_idx = ifm->ifi_index; |
| 3056 | |
| 3057 | if (br_idx) { |
| 3058 | br_dev = __dev_get_by_index(net, br_idx); |
| 3059 | if (!br_dev) |
| 3060 | return -ENODEV; |
| 3061 | |
| 3062 | ops = br_dev->netdev_ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3063 | } |
| 3064 | |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3065 | cb->args[1] = 0; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3066 | for_each_netdev(net, dev) { |
| 3067 | if (brport_idx && (dev->ifindex != brport_idx)) |
| 3068 | continue; |
| 3069 | |
| 3070 | if (!br_idx) { /* user did not specify a specific bridge */ |
| 3071 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 3072 | br_dev = netdev_master_upper_dev_get(dev); |
| 3073 | cops = br_dev->netdev_ops; |
| 3074 | } |
| 3075 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3076 | } else { |
| 3077 | if (dev != br_dev && |
| 3078 | !(dev->priv_flags & IFF_BRIDGE_PORT)) |
| 3079 | continue; |
| 3080 | |
| 3081 | if (br_dev != netdev_master_upper_dev_get(dev) && |
| 3082 | !(dev->priv_flags & IFF_EBRIDGE)) |
| 3083 | continue; |
| 3084 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3085 | cops = ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3088 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 3089 | if (cops && cops->ndo_fdb_dump) |
| 3090 | idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, |
| 3091 | idx); |
| 3092 | } |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3093 | if (cb->args[1] == -EMSGSIZE) |
| 3094 | break; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3095 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3096 | if (dev->netdev_ops->ndo_fdb_dump) |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 3097 | idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 3098 | idx); |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 3099 | else |
| 3100 | idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3101 | if (cb->args[1] == -EMSGSIZE) |
| 3102 | break; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3103 | |
| 3104 | cops = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3105 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3106 | |
| 3107 | cb->args[0] = idx; |
| 3108 | return skb->len; |
| 3109 | } |
| 3110 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3111 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
| 3112 | unsigned int attrnum, unsigned int flag) |
| 3113 | { |
| 3114 | if (mask & flag) |
| 3115 | return nla_put_u8(skb, attrnum, !!(flags & flag)); |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3119 | int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3120 | struct net_device *dev, u16 mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3121 | u32 flags, u32 mask, int nlflags, |
| 3122 | u32 filter_mask, |
| 3123 | int (*vlan_fill)(struct sk_buff *skb, |
| 3124 | struct net_device *dev, |
| 3125 | u32 filter_mask)) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3126 | { |
| 3127 | struct nlmsghdr *nlh; |
| 3128 | struct ifinfomsg *ifm; |
| 3129 | struct nlattr *br_afspec; |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3130 | struct nlattr *protinfo; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3131 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3132 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3133 | int err = 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3134 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 3135 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3136 | if (nlh == NULL) |
| 3137 | return -EMSGSIZE; |
| 3138 | |
| 3139 | ifm = nlmsg_data(nlh); |
| 3140 | ifm->ifi_family = AF_BRIDGE; |
| 3141 | ifm->__ifi_pad = 0; |
| 3142 | ifm->ifi_type = dev->type; |
| 3143 | ifm->ifi_index = dev->ifindex; |
| 3144 | ifm->ifi_flags = dev_get_flags(dev); |
| 3145 | ifm->ifi_change = 0; |
| 3146 | |
| 3147 | |
| 3148 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 3149 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 3150 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3151 | (br_dev && |
| 3152 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3153 | (dev->addr_len && |
| 3154 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 3155 | (dev->ifindex != dev_get_iflink(dev) && |
| 3156 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3157 | goto nla_put_failure; |
| 3158 | |
| 3159 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); |
| 3160 | if (!br_afspec) |
| 3161 | goto nla_put_failure; |
| 3162 | |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 3163 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3164 | nla_nest_cancel(skb, br_afspec); |
| 3165 | goto nla_put_failure; |
| 3166 | } |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 3167 | |
| 3168 | if (mode != BRIDGE_MODE_UNDEF) { |
| 3169 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { |
| 3170 | nla_nest_cancel(skb, br_afspec); |
| 3171 | goto nla_put_failure; |
| 3172 | } |
| 3173 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3174 | if (vlan_fill) { |
| 3175 | err = vlan_fill(skb, dev, filter_mask); |
| 3176 | if (err) { |
| 3177 | nla_nest_cancel(skb, br_afspec); |
| 3178 | goto nla_put_failure; |
| 3179 | } |
| 3180 | } |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3181 | nla_nest_end(skb, br_afspec); |
| 3182 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3183 | protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
| 3184 | if (!protinfo) |
| 3185 | goto nla_put_failure; |
| 3186 | |
| 3187 | if (brport_nla_put_flag(skb, flags, mask, |
| 3188 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || |
| 3189 | brport_nla_put_flag(skb, flags, mask, |
| 3190 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || |
| 3191 | brport_nla_put_flag(skb, flags, mask, |
| 3192 | IFLA_BRPORT_FAST_LEAVE, |
| 3193 | BR_MULTICAST_FAST_LEAVE) || |
| 3194 | brport_nla_put_flag(skb, flags, mask, |
| 3195 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || |
| 3196 | brport_nla_put_flag(skb, flags, mask, |
| 3197 | IFLA_BRPORT_LEARNING, BR_LEARNING) || |
| 3198 | brport_nla_put_flag(skb, flags, mask, |
| 3199 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || |
| 3200 | brport_nla_put_flag(skb, flags, mask, |
| 3201 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || |
| 3202 | brport_nla_put_flag(skb, flags, mask, |
| 3203 | IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { |
| 3204 | nla_nest_cancel(skb, protinfo); |
| 3205 | goto nla_put_failure; |
| 3206 | } |
| 3207 | |
| 3208 | nla_nest_end(skb, protinfo); |
| 3209 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3210 | nlmsg_end(skb, nlh); |
| 3211 | return 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3212 | nla_put_failure: |
| 3213 | nlmsg_cancel(skb, nlh); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3214 | return err ? err : -EMSGSIZE; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3215 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3216 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3217 | |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3218 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
| 3219 | { |
| 3220 | struct net *net = sock_net(skb->sk); |
| 3221 | struct net_device *dev; |
| 3222 | int idx = 0; |
| 3223 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 3224 | u32 seq = cb->nlh->nlmsg_seq; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 3225 | u32 filter_mask = 0; |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3226 | int err; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 3227 | |
Thomas Graf | aa68c20 | 2014-11-26 13:42:20 +0100 | [diff] [blame] | 3228 | if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { |
| 3229 | struct nlattr *extfilt; |
| 3230 | |
| 3231 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), |
| 3232 | IFLA_EXT_MASK); |
| 3233 | if (extfilt) { |
| 3234 | if (nla_len(extfilt) < sizeof(filter_mask)) |
| 3235 | return -EINVAL; |
| 3236 | |
| 3237 | filter_mask = nla_get_u32(extfilt); |
| 3238 | } |
| 3239 | } |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3240 | |
| 3241 | rcu_read_lock(); |
| 3242 | for_each_netdev_rcu(net, dev) { |
| 3243 | const struct net_device_ops *ops = dev->netdev_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3244 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3245 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3246 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3247 | if (idx >= cb->args[0]) { |
| 3248 | err = br_dev->netdev_ops->ndo_bridge_getlink( |
| 3249 | skb, portid, seq, dev, |
| 3250 | filter_mask, NLM_F_MULTI); |
| 3251 | if (err < 0 && err != -EOPNOTSUPP) |
| 3252 | break; |
| 3253 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 3254 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | if (ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3258 | if (idx >= cb->args[0]) { |
| 3259 | err = ops->ndo_bridge_getlink(skb, portid, |
| 3260 | seq, dev, |
| 3261 | filter_mask, |
| 3262 | NLM_F_MULTI); |
| 3263 | if (err < 0 && err != -EOPNOTSUPP) |
| 3264 | break; |
| 3265 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 3266 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3267 | } |
| 3268 | } |
| 3269 | rcu_read_unlock(); |
| 3270 | cb->args[0] = idx; |
| 3271 | |
| 3272 | return skb->len; |
| 3273 | } |
| 3274 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3275 | static inline size_t bridge_nlmsg_size(void) |
| 3276 | { |
| 3277 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 3278 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 3279 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 3280 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ |
| 3281 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ |
| 3282 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ |
| 3283 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ |
| 3284 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ |
| 3285 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ |
| 3286 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ |
| 3287 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ |
| 3288 | } |
| 3289 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3290 | static int rtnl_bridge_notify(struct net_device *dev) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3291 | { |
| 3292 | struct net *net = dev_net(dev); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3293 | struct sk_buff *skb; |
| 3294 | int err = -EOPNOTSUPP; |
| 3295 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3296 | if (!dev->netdev_ops->ndo_bridge_getlink) |
| 3297 | return 0; |
| 3298 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3299 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
| 3300 | if (!skb) { |
| 3301 | err = -ENOMEM; |
| 3302 | goto errout; |
| 3303 | } |
| 3304 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 3305 | err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3306 | if (err < 0) |
| 3307 | goto errout; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3308 | |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 3309 | if (!skb->len) |
| 3310 | goto errout; |
| 3311 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3312 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 3313 | return 0; |
| 3314 | errout: |
| 3315 | WARN_ON(err == -EMSGSIZE); |
| 3316 | kfree_skb(skb); |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 3317 | if (err) |
| 3318 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3319 | return err; |
| 3320 | } |
| 3321 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3322 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3323 | { |
| 3324 | struct net *net = sock_net(skb->sk); |
| 3325 | struct ifinfomsg *ifm; |
| 3326 | struct net_device *dev; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3327 | struct nlattr *br_spec, *attr = NULL; |
| 3328 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 3329 | u16 flags = 0; |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3330 | bool have_flags = false; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3331 | |
| 3332 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 3333 | return -EINVAL; |
| 3334 | |
| 3335 | ifm = nlmsg_data(nlh); |
| 3336 | if (ifm->ifi_family != AF_BRIDGE) |
| 3337 | return -EPFNOSUPPORT; |
| 3338 | |
| 3339 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 3340 | if (!dev) { |
| 3341 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 3342 | return -ENODEV; |
| 3343 | } |
| 3344 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3345 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 3346 | if (br_spec) { |
| 3347 | nla_for_each_nested(attr, br_spec, rem) { |
| 3348 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 3349 | if (nla_len(attr) < sizeof(flags)) |
| 3350 | return -EINVAL; |
| 3351 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3352 | have_flags = true; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3353 | flags = nla_get_u16(attr); |
| 3354 | break; |
| 3355 | } |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3360 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 3361 | |
| 3362 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3363 | err = -EOPNOTSUPP; |
| 3364 | goto out; |
| 3365 | } |
| 3366 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3367 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3368 | if (err) |
| 3369 | goto out; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3370 | |
| 3371 | flags &= ~BRIDGE_FLAGS_MASTER; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3374 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3375 | if (!dev->netdev_ops->ndo_bridge_setlink) |
| 3376 | err = -EOPNOTSUPP; |
| 3377 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3378 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
| 3379 | flags); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3380 | if (!err) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3381 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3382 | |
| 3383 | /* Generate event to notify upper layer of bridge |
| 3384 | * change |
| 3385 | */ |
| 3386 | err = rtnl_bridge_notify(dev); |
| 3387 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3390 | if (have_flags) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3391 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3392 | out: |
| 3393 | return err; |
| 3394 | } |
| 3395 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3396 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3397 | { |
| 3398 | struct net *net = sock_net(skb->sk); |
| 3399 | struct ifinfomsg *ifm; |
| 3400 | struct net_device *dev; |
| 3401 | struct nlattr *br_spec, *attr = NULL; |
| 3402 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 3403 | u16 flags = 0; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3404 | bool have_flags = false; |
| 3405 | |
| 3406 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 3407 | return -EINVAL; |
| 3408 | |
| 3409 | ifm = nlmsg_data(nlh); |
| 3410 | if (ifm->ifi_family != AF_BRIDGE) |
| 3411 | return -EPFNOSUPPORT; |
| 3412 | |
| 3413 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 3414 | if (!dev) { |
| 3415 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 3416 | return -ENODEV; |
| 3417 | } |
| 3418 | |
| 3419 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 3420 | if (br_spec) { |
| 3421 | nla_for_each_nested(attr, br_spec, rem) { |
| 3422 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 3423 | if (nla_len(attr) < sizeof(flags)) |
| 3424 | return -EINVAL; |
| 3425 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3426 | have_flags = true; |
| 3427 | flags = nla_get_u16(attr); |
| 3428 | break; |
| 3429 | } |
| 3430 | } |
| 3431 | } |
| 3432 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3433 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
| 3434 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 3435 | |
| 3436 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { |
| 3437 | err = -EOPNOTSUPP; |
| 3438 | goto out; |
| 3439 | } |
| 3440 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3441 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3442 | if (err) |
| 3443 | goto out; |
| 3444 | |
| 3445 | flags &= ~BRIDGE_FLAGS_MASTER; |
| 3446 | } |
| 3447 | |
| 3448 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3449 | if (!dev->netdev_ops->ndo_bridge_dellink) |
| 3450 | err = -EOPNOTSUPP; |
| 3451 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3452 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
| 3453 | flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3454 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3455 | if (!err) { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3456 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3457 | |
| 3458 | /* Generate event to notify upper layer of bridge |
| 3459 | * change |
| 3460 | */ |
| 3461 | err = rtnl_bridge_notify(dev); |
| 3462 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | if (have_flags) |
| 3466 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3467 | out: |
| 3468 | return err; |
| 3469 | } |
| 3470 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3471 | static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) |
| 3472 | { |
| 3473 | return (mask & IFLA_STATS_FILTER_BIT(attrid)) && |
| 3474 | (!idxattr || idxattr == attrid); |
| 3475 | } |
| 3476 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3477 | static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, |
| 3478 | int type, u32 pid, u32 seq, u32 change, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3479 | unsigned int flags, unsigned int filter_mask, |
| 3480 | int *idxattr, int *prividx) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3481 | { |
| 3482 | struct if_stats_msg *ifsm; |
| 3483 | struct nlmsghdr *nlh; |
| 3484 | struct nlattr *attr; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3485 | int s_prividx = *prividx; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3486 | |
| 3487 | ASSERT_RTNL(); |
| 3488 | |
| 3489 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); |
| 3490 | if (!nlh) |
| 3491 | return -EMSGSIZE; |
| 3492 | |
| 3493 | ifsm = nlmsg_data(nlh); |
| 3494 | ifsm->ifindex = dev->ifindex; |
| 3495 | ifsm->filter_mask = filter_mask; |
| 3496 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3497 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3498 | struct rtnl_link_stats64 *sp; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3499 | |
Nicolas Dichtel | 58414d3 | 2016-04-21 18:58:25 +0200 | [diff] [blame] | 3500 | attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, |
| 3501 | sizeof(struct rtnl_link_stats64), |
| 3502 | IFLA_STATS_UNSPEC); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3503 | if (!attr) |
| 3504 | goto nla_put_failure; |
| 3505 | |
| 3506 | sp = nla_data(attr); |
| 3507 | dev_get_stats(dev, sp); |
| 3508 | } |
| 3509 | |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 3510 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { |
| 3511 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 3512 | |
| 3513 | if (ops && ops->fill_linkxstats) { |
| 3514 | int err; |
| 3515 | |
| 3516 | *idxattr = IFLA_STATS_LINK_XSTATS; |
| 3517 | attr = nla_nest_start(skb, |
| 3518 | IFLA_STATS_LINK_XSTATS); |
| 3519 | if (!attr) |
| 3520 | goto nla_put_failure; |
| 3521 | |
| 3522 | err = ops->fill_linkxstats(skb, dev, prividx); |
| 3523 | nla_nest_end(skb, attr); |
| 3524 | if (err) |
| 3525 | goto nla_put_failure; |
| 3526 | *idxattr = 0; |
| 3527 | } |
| 3528 | } |
| 3529 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3530 | nlmsg_end(skb, nlh); |
| 3531 | |
| 3532 | return 0; |
| 3533 | |
| 3534 | nla_put_failure: |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3535 | /* not a multi message or no progress mean a real error */ |
| 3536 | if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) |
| 3537 | nlmsg_cancel(skb, nlh); |
| 3538 | else |
| 3539 | nlmsg_end(skb, nlh); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3540 | |
| 3541 | return -EMSGSIZE; |
| 3542 | } |
| 3543 | |
| 3544 | static const struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = { |
| 3545 | [IFLA_STATS_LINK_64] = { .len = sizeof(struct rtnl_link_stats64) }, |
| 3546 | }; |
| 3547 | |
| 3548 | static size_t if_nlmsg_stats_size(const struct net_device *dev, |
| 3549 | u32 filter_mask) |
| 3550 | { |
| 3551 | size_t size = 0; |
| 3552 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3553 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3554 | size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); |
| 3555 | |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 3556 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { |
| 3557 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 3558 | |
| 3559 | if (ops && ops->get_linkxstats_size) { |
| 3560 | size += nla_total_size(ops->get_linkxstats_size(dev)); |
| 3561 | /* for IFLA_STATS_LINK_XSTATS */ |
| 3562 | size += nla_total_size(0); |
| 3563 | } |
| 3564 | } |
| 3565 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3566 | return size; |
| 3567 | } |
| 3568 | |
| 3569 | static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 3570 | { |
| 3571 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3572 | struct net_device *dev = NULL; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3573 | int idxattr = 0, prividx = 0; |
| 3574 | struct if_stats_msg *ifsm; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3575 | struct sk_buff *nskb; |
| 3576 | u32 filter_mask; |
| 3577 | int err; |
| 3578 | |
| 3579 | ifsm = nlmsg_data(nlh); |
| 3580 | if (ifsm->ifindex > 0) |
| 3581 | dev = __dev_get_by_index(net, ifsm->ifindex); |
| 3582 | else |
| 3583 | return -EINVAL; |
| 3584 | |
| 3585 | if (!dev) |
| 3586 | return -ENODEV; |
| 3587 | |
| 3588 | filter_mask = ifsm->filter_mask; |
| 3589 | if (!filter_mask) |
| 3590 | return -EINVAL; |
| 3591 | |
| 3592 | nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); |
| 3593 | if (!nskb) |
| 3594 | return -ENOBUFS; |
| 3595 | |
| 3596 | err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, |
| 3597 | NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3598 | 0, filter_mask, &idxattr, &prividx); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3599 | if (err < 0) { |
| 3600 | /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ |
| 3601 | WARN_ON(err == -EMSGSIZE); |
| 3602 | kfree_skb(nskb); |
| 3603 | } else { |
| 3604 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
| 3605 | } |
| 3606 | |
| 3607 | return err; |
| 3608 | } |
| 3609 | |
| 3610 | static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 3611 | { |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3612 | int h, s_h, err, s_idx, s_idxattr, s_prividx; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3613 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3614 | unsigned int flags = NLM_F_MULTI; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3615 | struct if_stats_msg *ifsm; |
| 3616 | struct hlist_head *head; |
| 3617 | struct net_device *dev; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3618 | u32 filter_mask = 0; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3619 | int idx = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3620 | |
| 3621 | s_h = cb->args[0]; |
| 3622 | s_idx = cb->args[1]; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3623 | s_idxattr = cb->args[2]; |
| 3624 | s_prividx = cb->args[3]; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3625 | |
| 3626 | cb->seq = net->dev_base_seq; |
| 3627 | |
| 3628 | ifsm = nlmsg_data(cb->nlh); |
| 3629 | filter_mask = ifsm->filter_mask; |
| 3630 | if (!filter_mask) |
| 3631 | return -EINVAL; |
| 3632 | |
| 3633 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 3634 | idx = 0; |
| 3635 | head = &net->dev_index_head[h]; |
| 3636 | hlist_for_each_entry(dev, head, index_hlist) { |
| 3637 | if (idx < s_idx) |
| 3638 | goto cont; |
| 3639 | err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, |
| 3640 | NETLINK_CB(cb->skb).portid, |
| 3641 | cb->nlh->nlmsg_seq, 0, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3642 | flags, filter_mask, |
| 3643 | &s_idxattr, &s_prividx); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3644 | /* If we ran out of room on the first message, |
| 3645 | * we're in trouble |
| 3646 | */ |
| 3647 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); |
| 3648 | |
| 3649 | if (err < 0) |
| 3650 | goto out; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3651 | s_prividx = 0; |
| 3652 | s_idxattr = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3653 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| 3654 | cont: |
| 3655 | idx++; |
| 3656 | } |
| 3657 | } |
| 3658 | out: |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 3659 | cb->args[3] = s_prividx; |
| 3660 | cb->args[2] = s_idxattr; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3661 | cb->args[1] = idx; |
| 3662 | cb->args[0] = h; |
| 3663 | |
| 3664 | return skb->len; |
| 3665 | } |
| 3666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | /* Process one rtnetlink message. */ |
| 3668 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3669 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3671 | struct net *net = sock_net(skb->sk); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3672 | rtnl_doit_func doit; |
Alexander Kuleshov | 617cfc7 | 2016-01-10 21:26:57 +0600 | [diff] [blame] | 3673 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | int family; |
| 3675 | int type; |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3676 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3680 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | |
| 3682 | type -= RTM_BASE; |
| 3683 | |
| 3684 | /* All the messages must have at least 1 byte length */ |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3685 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | return 0; |
| 3687 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3688 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | kind = type&3; |
| 3690 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 3691 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3692 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 3694 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3695 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3696 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3697 | rtnl_calcit_func calcit; |
| 3698 | u16 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3700 | dumpit = rtnl_get_dumpit(family, type); |
| 3701 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3702 | return -EOPNOTSUPP; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3703 | calcit = rtnl_get_calcit(family, type); |
| 3704 | if (calcit) |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3705 | min_dump_alloc = calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3707 | __rtnl_unlock(); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3708 | rtnl = net->rtnl; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 3709 | { |
| 3710 | struct netlink_dump_control c = { |
| 3711 | .dump = dumpit, |
| 3712 | .min_dump_alloc = min_dump_alloc, |
| 3713 | }; |
| 3714 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
| 3715 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3716 | rtnl_lock(); |
| 3717 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | } |
| 3719 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3720 | doit = rtnl_get_doit(family, type); |
| 3721 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3722 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3724 | return doit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | } |
| 3726 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3727 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3729 | rtnl_lock(); |
| 3730 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
| 3731 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | } |
| 3733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 3735 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3736 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 3737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | switch (event) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | case NETDEV_UP: |
| 3740 | case NETDEV_DOWN: |
Patrick McHardy | 10de05a | 2010-02-26 06:34:50 +0000 | [diff] [blame] | 3741 | case NETDEV_PRE_UP: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 3742 | case NETDEV_POST_INIT: |
| 3743 | case NETDEV_REGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | case NETDEV_CHANGE: |
Patrick McHardy | 755d0e7 | 2010-03-19 04:42:24 +0000 | [diff] [blame] | 3745 | case NETDEV_PRE_TYPE_CHANGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | case NETDEV_GOING_DOWN: |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 3747 | case NETDEV_UNREGISTER: |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 3748 | case NETDEV_UNREGISTER_FINAL: |
Amerigo Wang | ac3d3f8 | 2011-05-19 23:06:32 +0000 | [diff] [blame] | 3749 | case NETDEV_RELEASE: |
| 3750 | case NETDEV_JOIN: |
Moni Shoua | 61bd385 | 2015-02-03 16:48:29 +0200 | [diff] [blame] | 3751 | case NETDEV_BONDING_INFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | break; |
| 3753 | default: |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 3754 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | break; |
| 3756 | } |
| 3757 | return NOTIFY_DONE; |
| 3758 | } |
| 3759 | |
| 3760 | static struct notifier_block rtnetlink_dev_notifier = { |
| 3761 | .notifier_call = rtnetlink_event, |
| 3762 | }; |
| 3763 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3764 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3765 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3766 | { |
| 3767 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3768 | struct netlink_kernel_cfg cfg = { |
| 3769 | .groups = RTNLGRP_MAX, |
| 3770 | .input = rtnetlink_rcv, |
| 3771 | .cb_mutex = &rtnl_mutex, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 3772 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3773 | }; |
| 3774 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 3775 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3776 | if (!sk) |
| 3777 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3778 | net->rtnl = sk; |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3782 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3783 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 3784 | netlink_kernel_release(net->rtnl); |
| 3785 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | static struct pernet_operations rtnetlink_net_ops = { |
| 3789 | .init = rtnetlink_net_init, |
| 3790 | .exit = rtnetlink_net_exit, |
| 3791 | }; |
| 3792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | void __init rtnetlink_init(void) |
| 3794 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3795 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 3799 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3800 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
| 3801 | rtnl_dump_ifinfo, rtnl_calcit); |
| 3802 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); |
| 3803 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); |
| 3804 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 3805 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3806 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
| 3807 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3808 | |
| 3809 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); |
| 3810 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); |
| 3811 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3812 | |
| 3813 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3814 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3815 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 3816 | |
| 3817 | rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, |
| 3818 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | } |