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