• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  *
15  *	Fixes:
16  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
17  */
18 
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/pci.h>
40 #include <linux/etherdevice.h>
41 
42 #include <asm/uaccess.h>
43 
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <net/ip.h>
47 #include <net/protocol.h>
48 #include <net/arp.h>
49 #include <net/route.h>
50 #include <net/udp.h>
51 #include <net/sock.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
56 
57 struct rtnl_link {
58 	rtnl_doit_func		doit;
59 	rtnl_dumpit_func	dumpit;
60 	rtnl_calcit_func 	calcit;
61 };
62 
63 static DEFINE_MUTEX(rtnl_mutex);
64 
rtnl_lock(void)65 void rtnl_lock(void)
66 {
67 	mutex_lock(&rtnl_mutex);
68 }
69 EXPORT_SYMBOL(rtnl_lock);
70 
__rtnl_unlock(void)71 void __rtnl_unlock(void)
72 {
73 	mutex_unlock(&rtnl_mutex);
74 }
75 
rtnl_unlock(void)76 void rtnl_unlock(void)
77 {
78 	/* This fellow will unlock it for us. */
79 	netdev_run_todo();
80 }
81 EXPORT_SYMBOL(rtnl_unlock);
82 
rtnl_trylock(void)83 int rtnl_trylock(void)
84 {
85 	return mutex_trylock(&rtnl_mutex);
86 }
87 EXPORT_SYMBOL(rtnl_trylock);
88 
rtnl_is_locked(void)89 int rtnl_is_locked(void)
90 {
91 	return mutex_is_locked(&rtnl_mutex);
92 }
93 EXPORT_SYMBOL(rtnl_is_locked);
94 
95 #ifdef CONFIG_PROVE_LOCKING
lockdep_rtnl_is_held(void)96 int lockdep_rtnl_is_held(void)
97 {
98 	return lockdep_is_held(&rtnl_mutex);
99 }
100 EXPORT_SYMBOL(lockdep_rtnl_is_held);
101 #endif /* #ifdef CONFIG_PROVE_LOCKING */
102 
103 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
104 
rtm_msgindex(int msgtype)105 static inline int rtm_msgindex(int msgtype)
106 {
107 	int msgindex = msgtype - RTM_BASE;
108 
109 	/*
110 	 * msgindex < 0 implies someone tried to register a netlink
111 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112 	 * the message type has not been added to linux/rtnetlink.h
113 	 */
114 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
115 
116 	return msgindex;
117 }
118 
rtnl_get_doit(int protocol,int msgindex)119 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120 {
121 	struct rtnl_link *tab;
122 
123 	if (protocol <= RTNL_FAMILY_MAX)
124 		tab = rtnl_msg_handlers[protocol];
125 	else
126 		tab = NULL;
127 
128 	if (tab == NULL || tab[msgindex].doit == NULL)
129 		tab = rtnl_msg_handlers[PF_UNSPEC];
130 
131 	return tab[msgindex].doit;
132 }
133 
rtnl_get_dumpit(int protocol,int msgindex)134 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135 {
136 	struct rtnl_link *tab;
137 
138 	if (protocol <= RTNL_FAMILY_MAX)
139 		tab = rtnl_msg_handlers[protocol];
140 	else
141 		tab = NULL;
142 
143 	if (tab == NULL || tab[msgindex].dumpit == NULL)
144 		tab = rtnl_msg_handlers[PF_UNSPEC];
145 
146 	return tab[msgindex].dumpit;
147 }
148 
rtnl_get_calcit(int protocol,int msgindex)149 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150 {
151 	struct rtnl_link *tab;
152 
153 	if (protocol <= RTNL_FAMILY_MAX)
154 		tab = rtnl_msg_handlers[protocol];
155 	else
156 		tab = NULL;
157 
158 	if (tab == NULL || tab[msgindex].calcit == NULL)
159 		tab = rtnl_msg_handlers[PF_UNSPEC];
160 
161 	return tab[msgindex].calcit;
162 }
163 
164 /**
165  * __rtnl_register - Register a rtnetlink message type
166  * @protocol: Protocol family or PF_UNSPEC
167  * @msgtype: rtnetlink message type
168  * @doit: Function pointer called for each request message
169  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170  * @calcit: Function pointer to calc size of dump message
171  *
172  * Registers the specified function pointers (at least one of them has
173  * to be non-NULL) to be called whenever a request message for the
174  * specified protocol family and message type is received.
175  *
176  * The special protocol family PF_UNSPEC may be used to define fallback
177  * function pointers for the case when no entry for the specific protocol
178  * family exists.
179  *
180  * Returns 0 on success or a negative error code.
181  */
__rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,rtnl_calcit_func calcit)182 int __rtnl_register(int protocol, int msgtype,
183 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184 		    rtnl_calcit_func calcit)
185 {
186 	struct rtnl_link *tab;
187 	int msgindex;
188 
189 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190 	msgindex = rtm_msgindex(msgtype);
191 
192 	tab = rtnl_msg_handlers[protocol];
193 	if (tab == NULL) {
194 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195 		if (tab == NULL)
196 			return -ENOBUFS;
197 
198 		rtnl_msg_handlers[protocol] = tab;
199 	}
200 
201 	if (doit)
202 		tab[msgindex].doit = doit;
203 
204 	if (dumpit)
205 		tab[msgindex].dumpit = dumpit;
206 
207 	if (calcit)
208 		tab[msgindex].calcit = calcit;
209 
210 	return 0;
211 }
212 EXPORT_SYMBOL_GPL(__rtnl_register);
213 
214 /**
215  * rtnl_register - Register a rtnetlink message type
216  *
217  * Identical to __rtnl_register() but panics on failure. This is useful
218  * as failure of this function is very unlikely, it can only happen due
219  * to lack of memory when allocating the chain to store all message
220  * handlers for a protocol. Meant for use in init functions where lack
221  * of memory implies no sense in continuing.
222  */
rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,rtnl_calcit_func calcit)223 void rtnl_register(int protocol, int msgtype,
224 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225 		   rtnl_calcit_func calcit)
226 {
227 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228 		panic("Unable to register rtnetlink message handler, "
229 		      "protocol = %d, message type = %d\n",
230 		      protocol, msgtype);
231 }
232 EXPORT_SYMBOL_GPL(rtnl_register);
233 
234 /**
235  * rtnl_unregister - Unregister a rtnetlink message type
236  * @protocol: Protocol family or PF_UNSPEC
237  * @msgtype: rtnetlink message type
238  *
239  * Returns 0 on success or a negative error code.
240  */
rtnl_unregister(int protocol,int msgtype)241 int rtnl_unregister(int protocol, int msgtype)
242 {
243 	int msgindex;
244 
245 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246 	msgindex = rtm_msgindex(msgtype);
247 
248 	if (rtnl_msg_handlers[protocol] == NULL)
249 		return -ENOENT;
250 
251 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253 
254 	return 0;
255 }
256 EXPORT_SYMBOL_GPL(rtnl_unregister);
257 
258 /**
259  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260  * @protocol : Protocol family or PF_UNSPEC
261  *
262  * Identical to calling rtnl_unregster() for all registered message types
263  * of a certain protocol family.
264  */
rtnl_unregister_all(int protocol)265 void rtnl_unregister_all(int protocol)
266 {
267 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268 
269 	kfree(rtnl_msg_handlers[protocol]);
270 	rtnl_msg_handlers[protocol] = NULL;
271 }
272 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
273 
274 static LIST_HEAD(link_ops);
275 
rtnl_link_ops_get(const char * kind)276 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277 {
278 	const struct rtnl_link_ops *ops;
279 
280 	list_for_each_entry(ops, &link_ops, list) {
281 		if (!strcmp(ops->kind, kind))
282 			return ops;
283 	}
284 	return NULL;
285 }
286 
287 /**
288  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
289  * @ops: struct rtnl_link_ops * to register
290  *
291  * The caller must hold the rtnl_mutex. This function should be used
292  * by drivers that create devices during module initialization. It
293  * must be called before registering the devices.
294  *
295  * Returns 0 on success or a negative error code.
296  */
__rtnl_link_register(struct rtnl_link_ops * ops)297 int __rtnl_link_register(struct rtnl_link_ops *ops)
298 {
299 	if (rtnl_link_ops_get(ops->kind))
300 		return -EEXIST;
301 
302 	if (!ops->dellink)
303 		ops->dellink = unregister_netdevice_queue;
304 
305 	list_add_tail(&ops->list, &link_ops);
306 	return 0;
307 }
308 EXPORT_SYMBOL_GPL(__rtnl_link_register);
309 
310 /**
311  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
312  * @ops: struct rtnl_link_ops * to register
313  *
314  * Returns 0 on success or a negative error code.
315  */
rtnl_link_register(struct rtnl_link_ops * ops)316 int rtnl_link_register(struct rtnl_link_ops *ops)
317 {
318 	int err;
319 
320 	rtnl_lock();
321 	err = __rtnl_link_register(ops);
322 	rtnl_unlock();
323 	return err;
324 }
325 EXPORT_SYMBOL_GPL(rtnl_link_register);
326 
__rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)327 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
328 {
329 	struct net_device *dev;
330 	LIST_HEAD(list_kill);
331 
332 	for_each_netdev(net, dev) {
333 		if (dev->rtnl_link_ops == ops)
334 			ops->dellink(dev, &list_kill);
335 	}
336 	unregister_netdevice_many(&list_kill);
337 }
338 
339 /**
340  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
341  * @ops: struct rtnl_link_ops * to unregister
342  *
343  * The caller must hold the rtnl_mutex.
344  */
__rtnl_link_unregister(struct rtnl_link_ops * ops)345 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
346 {
347 	struct net *net;
348 
349 	for_each_net(net) {
350 		__rtnl_kill_links(net, ops);
351 	}
352 	list_del(&ops->list);
353 }
354 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
355 
356 /**
357  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
358  * @ops: struct rtnl_link_ops * to unregister
359  */
rtnl_link_unregister(struct rtnl_link_ops * ops)360 void rtnl_link_unregister(struct rtnl_link_ops *ops)
361 {
362 	rtnl_lock();
363 	__rtnl_link_unregister(ops);
364 	rtnl_unlock();
365 }
366 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
367 
rtnl_link_get_size(const struct net_device * dev)368 static size_t rtnl_link_get_size(const struct net_device *dev)
369 {
370 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
371 	size_t size;
372 
373 	if (!ops)
374 		return 0;
375 
376 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
378 
379 	if (ops->get_size)
380 		/* IFLA_INFO_DATA + nested data */
381 		size += nla_total_size(sizeof(struct nlattr)) +
382 			ops->get_size(dev);
383 
384 	if (ops->get_xstats_size)
385 		/* IFLA_INFO_XSTATS */
386 		size += nla_total_size(ops->get_xstats_size(dev));
387 
388 	return size;
389 }
390 
391 static LIST_HEAD(rtnl_af_ops);
392 
rtnl_af_lookup(const int family)393 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
394 {
395 	const struct rtnl_af_ops *ops;
396 
397 	list_for_each_entry(ops, &rtnl_af_ops, list) {
398 		if (ops->family == family)
399 			return ops;
400 	}
401 
402 	return NULL;
403 }
404 
405 /**
406  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407  * @ops: struct rtnl_af_ops * to register
408  *
409  * The caller must hold the rtnl_mutex.
410  *
411  * Returns 0 on success or a negative error code.
412  */
__rtnl_af_register(struct rtnl_af_ops * ops)413 int __rtnl_af_register(struct rtnl_af_ops *ops)
414 {
415 	list_add_tail(&ops->list, &rtnl_af_ops);
416 	return 0;
417 }
418 EXPORT_SYMBOL_GPL(__rtnl_af_register);
419 
420 /**
421  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
422  * @ops: struct rtnl_af_ops * to register
423  *
424  * Returns 0 on success or a negative error code.
425  */
rtnl_af_register(struct rtnl_af_ops * ops)426 int rtnl_af_register(struct rtnl_af_ops *ops)
427 {
428 	int err;
429 
430 	rtnl_lock();
431 	err = __rtnl_af_register(ops);
432 	rtnl_unlock();
433 	return err;
434 }
435 EXPORT_SYMBOL_GPL(rtnl_af_register);
436 
437 /**
438  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
439  * @ops: struct rtnl_af_ops * to unregister
440  *
441  * The caller must hold the rtnl_mutex.
442  */
__rtnl_af_unregister(struct rtnl_af_ops * ops)443 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
444 {
445 	list_del(&ops->list);
446 }
447 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
448 
449 /**
450  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
451  * @ops: struct rtnl_af_ops * to unregister
452  */
rtnl_af_unregister(struct rtnl_af_ops * ops)453 void rtnl_af_unregister(struct rtnl_af_ops *ops)
454 {
455 	rtnl_lock();
456 	__rtnl_af_unregister(ops);
457 	rtnl_unlock();
458 }
459 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
460 
rtnl_link_get_af_size(const struct net_device * dev)461 static size_t rtnl_link_get_af_size(const struct net_device *dev)
462 {
463 	struct rtnl_af_ops *af_ops;
464 	size_t size;
465 
466 	/* IFLA_AF_SPEC */
467 	size = nla_total_size(sizeof(struct nlattr));
468 
469 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
470 		if (af_ops->get_link_af_size) {
471 			/* AF_* + nested data */
472 			size += nla_total_size(sizeof(struct nlattr)) +
473 				af_ops->get_link_af_size(dev);
474 		}
475 	}
476 
477 	return size;
478 }
479 
rtnl_link_fill(struct sk_buff * skb,const struct net_device * dev)480 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
481 {
482 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
483 	struct nlattr *linkinfo, *data;
484 	int err = -EMSGSIZE;
485 
486 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
487 	if (linkinfo == NULL)
488 		goto out;
489 
490 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
491 		goto err_cancel_link;
492 	if (ops->fill_xstats) {
493 		err = ops->fill_xstats(skb, dev);
494 		if (err < 0)
495 			goto err_cancel_link;
496 	}
497 	if (ops->fill_info) {
498 		data = nla_nest_start(skb, IFLA_INFO_DATA);
499 		if (data == NULL) {
500 			err = -EMSGSIZE;
501 			goto err_cancel_link;
502 		}
503 		err = ops->fill_info(skb, dev);
504 		if (err < 0)
505 			goto err_cancel_data;
506 		nla_nest_end(skb, data);
507 	}
508 
509 	nla_nest_end(skb, linkinfo);
510 	return 0;
511 
512 err_cancel_data:
513 	nla_nest_cancel(skb, data);
514 err_cancel_link:
515 	nla_nest_cancel(skb, linkinfo);
516 out:
517 	return err;
518 }
519 
rtnetlink_send(struct sk_buff * skb,struct net * net,u32 pid,unsigned int group,int echo)520 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
521 {
522 	struct sock *rtnl = net->rtnl;
523 	int err = 0;
524 
525 	NETLINK_CB(skb).dst_group = group;
526 	if (echo)
527 		atomic_inc(&skb->users);
528 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
529 	if (echo)
530 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
531 	return err;
532 }
533 
rtnl_unicast(struct sk_buff * skb,struct net * net,u32 pid)534 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
535 {
536 	struct sock *rtnl = net->rtnl;
537 
538 	return nlmsg_unicast(rtnl, skb, pid);
539 }
540 EXPORT_SYMBOL(rtnl_unicast);
541 
rtnl_notify(struct sk_buff * skb,struct net * net,u32 pid,u32 group,struct nlmsghdr * nlh,gfp_t flags)542 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
543 		 struct nlmsghdr *nlh, gfp_t flags)
544 {
545 	struct sock *rtnl = net->rtnl;
546 	int report = 0;
547 
548 	if (nlh)
549 		report = nlmsg_report(nlh);
550 
551 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
552 }
553 EXPORT_SYMBOL(rtnl_notify);
554 
rtnl_set_sk_err(struct net * net,u32 group,int error)555 void rtnl_set_sk_err(struct net *net, u32 group, int error)
556 {
557 	struct sock *rtnl = net->rtnl;
558 
559 	netlink_set_err(rtnl, 0, group, error);
560 }
561 EXPORT_SYMBOL(rtnl_set_sk_err);
562 
rtnetlink_put_metrics(struct sk_buff * skb,u32 * metrics)563 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
564 {
565 	struct nlattr *mx;
566 	int i, valid = 0;
567 
568 	mx = nla_nest_start(skb, RTA_METRICS);
569 	if (mx == NULL)
570 		return -ENOBUFS;
571 
572 	for (i = 0; i < RTAX_MAX; i++) {
573 		if (metrics[i]) {
574 			valid++;
575 			if (nla_put_u32(skb, i+1, metrics[i]))
576 				goto nla_put_failure;
577 		}
578 	}
579 
580 	if (!valid) {
581 		nla_nest_cancel(skb, mx);
582 		return 0;
583 	}
584 
585 	return nla_nest_end(skb, mx);
586 
587 nla_put_failure:
588 	nla_nest_cancel(skb, mx);
589 	return -EMSGSIZE;
590 }
591 EXPORT_SYMBOL(rtnetlink_put_metrics);
592 
rtnl_put_cacheinfo(struct sk_buff * skb,struct dst_entry * dst,u32 id,long expires,u32 error)593 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
594 		       long expires, u32 error)
595 {
596 	struct rta_cacheinfo ci = {
597 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
598 		.rta_used = dst->__use,
599 		.rta_clntref = atomic_read(&(dst->__refcnt)),
600 		.rta_error = error,
601 		.rta_id =  id,
602 	};
603 
604 	if (expires) {
605 		unsigned long clock;
606 
607 		clock = jiffies_to_clock_t(abs(expires));
608 		clock = min_t(unsigned long, clock, INT_MAX);
609 		ci.rta_expires = (expires > 0) ? clock : -clock;
610 	}
611 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
612 }
613 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
614 
set_operstate(struct net_device * dev,unsigned char transition)615 static void set_operstate(struct net_device *dev, unsigned char transition)
616 {
617 	unsigned char operstate = dev->operstate;
618 
619 	switch (transition) {
620 	case IF_OPER_UP:
621 		if ((operstate == IF_OPER_DORMANT ||
622 		     operstate == IF_OPER_UNKNOWN) &&
623 		    !netif_dormant(dev))
624 			operstate = IF_OPER_UP;
625 		break;
626 
627 	case IF_OPER_DORMANT:
628 		if (operstate == IF_OPER_UP ||
629 		    operstate == IF_OPER_UNKNOWN)
630 			operstate = IF_OPER_DORMANT;
631 		break;
632 	}
633 
634 	if (dev->operstate != operstate) {
635 		write_lock_bh(&dev_base_lock);
636 		dev->operstate = operstate;
637 		write_unlock_bh(&dev_base_lock);
638 		netdev_state_change(dev);
639 	}
640 }
641 
rtnl_dev_get_flags(const struct net_device * dev)642 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
643 {
644 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
645 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
646 }
647 
rtnl_dev_combine_flags(const struct net_device * dev,const struct ifinfomsg * ifm)648 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
649 					   const struct ifinfomsg *ifm)
650 {
651 	unsigned int flags = ifm->ifi_flags;
652 
653 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
654 	if (ifm->ifi_change)
655 		flags = (flags & ifm->ifi_change) |
656 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
657 
658 	return flags;
659 }
660 
copy_rtnl_link_stats(struct rtnl_link_stats * a,const struct rtnl_link_stats64 * b)661 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
662 				 const struct rtnl_link_stats64 *b)
663 {
664 	a->rx_packets = b->rx_packets;
665 	a->tx_packets = b->tx_packets;
666 	a->rx_bytes = b->rx_bytes;
667 	a->tx_bytes = b->tx_bytes;
668 	a->rx_errors = b->rx_errors;
669 	a->tx_errors = b->tx_errors;
670 	a->rx_dropped = b->rx_dropped;
671 	a->tx_dropped = b->tx_dropped;
672 
673 	a->multicast = b->multicast;
674 	a->collisions = b->collisions;
675 
676 	a->rx_length_errors = b->rx_length_errors;
677 	a->rx_over_errors = b->rx_over_errors;
678 	a->rx_crc_errors = b->rx_crc_errors;
679 	a->rx_frame_errors = b->rx_frame_errors;
680 	a->rx_fifo_errors = b->rx_fifo_errors;
681 	a->rx_missed_errors = b->rx_missed_errors;
682 
683 	a->tx_aborted_errors = b->tx_aborted_errors;
684 	a->tx_carrier_errors = b->tx_carrier_errors;
685 	a->tx_fifo_errors = b->tx_fifo_errors;
686 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
687 	a->tx_window_errors = b->tx_window_errors;
688 
689 	a->rx_compressed = b->rx_compressed;
690 	a->tx_compressed = b->tx_compressed;
691 }
692 
copy_rtnl_link_stats64(void * v,const struct rtnl_link_stats64 * b)693 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
694 {
695 	memcpy(v, b, sizeof(*b));
696 }
697 
698 /* All VF info */
rtnl_vfinfo_size(const struct net_device * dev,u32 ext_filter_mask)699 static inline int rtnl_vfinfo_size(const struct net_device *dev,
700 				   u32 ext_filter_mask)
701 {
702 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
703 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
704 		int num_vfs = dev_num_vf(dev->dev.parent);
705 		size_t size = nla_total_size(sizeof(struct nlattr));
706 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
707 		size += num_vfs *
708 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
709 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
710 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
711 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
712 		return size;
713 	} else
714 		return 0;
715 }
716 
rtnl_port_size(const struct net_device * dev)717 static size_t rtnl_port_size(const struct net_device *dev)
718 {
719 	size_t port_size = nla_total_size(4)		/* PORT_VF */
720 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
721 		+ nla_total_size(sizeof(struct ifla_port_vsi))
722 							/* PORT_VSI_TYPE */
723 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
724 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
725 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
726 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
727 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
728 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
729 		+ port_size;
730 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
731 		+ port_size;
732 
733 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
734 		return 0;
735 	if (dev_num_vf(dev->dev.parent))
736 		return port_self_size + vf_ports_size +
737 			vf_port_size * dev_num_vf(dev->dev.parent);
738 	else
739 		return port_self_size;
740 }
741 
if_nlmsg_size(const struct net_device * dev,u32 ext_filter_mask)742 static noinline size_t if_nlmsg_size(const struct net_device *dev,
743 				     u32 ext_filter_mask)
744 {
745 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
746 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
747 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
748 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
749 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
750 	       + nla_total_size(sizeof(struct rtnl_link_stats))
751 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
752 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
753 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
754 	       + nla_total_size(4) /* IFLA_TXQLEN */
755 	       + nla_total_size(4) /* IFLA_WEIGHT */
756 	       + nla_total_size(4) /* IFLA_MTU */
757 	       + nla_total_size(4) /* IFLA_LINK */
758 	       + nla_total_size(4) /* IFLA_MASTER */
759 	       + nla_total_size(1) /* IFLA_CARRIER */
760 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
761 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
762 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
763 	       + nla_total_size(1) /* IFLA_OPERSTATE */
764 	       + nla_total_size(1) /* IFLA_LINKMODE */
765 	       + nla_total_size(ext_filter_mask
766 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
767 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
768 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
769 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
770 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
771 }
772 
rtnl_vf_ports_fill(struct sk_buff * skb,struct net_device * dev)773 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
774 {
775 	struct nlattr *vf_ports;
776 	struct nlattr *vf_port;
777 	int vf;
778 	int err;
779 
780 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
781 	if (!vf_ports)
782 		return -EMSGSIZE;
783 
784 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
785 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
786 		if (!vf_port)
787 			goto nla_put_failure;
788 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
789 			goto nla_put_failure;
790 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
791 		if (err == -EMSGSIZE)
792 			goto nla_put_failure;
793 		if (err) {
794 			nla_nest_cancel(skb, vf_port);
795 			continue;
796 		}
797 		nla_nest_end(skb, vf_port);
798 	}
799 
800 	nla_nest_end(skb, vf_ports);
801 
802 	return 0;
803 
804 nla_put_failure:
805 	nla_nest_cancel(skb, vf_ports);
806 	return -EMSGSIZE;
807 }
808 
rtnl_port_self_fill(struct sk_buff * skb,struct net_device * dev)809 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
810 {
811 	struct nlattr *port_self;
812 	int err;
813 
814 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
815 	if (!port_self)
816 		return -EMSGSIZE;
817 
818 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
819 	if (err) {
820 		nla_nest_cancel(skb, port_self);
821 		return (err == -EMSGSIZE) ? err : 0;
822 	}
823 
824 	nla_nest_end(skb, port_self);
825 
826 	return 0;
827 }
828 
rtnl_port_fill(struct sk_buff * skb,struct net_device * dev)829 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
830 {
831 	int err;
832 
833 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
834 		return 0;
835 
836 	err = rtnl_port_self_fill(skb, dev);
837 	if (err)
838 		return err;
839 
840 	if (dev_num_vf(dev->dev.parent)) {
841 		err = rtnl_vf_ports_fill(skb, dev);
842 		if (err)
843 			return err;
844 	}
845 
846 	return 0;
847 }
848 
rtnl_fill_ifinfo(struct sk_buff * skb,struct net_device * dev,int type,u32 pid,u32 seq,u32 change,unsigned int flags,u32 ext_filter_mask)849 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
850 			    int type, u32 pid, u32 seq, u32 change,
851 			    unsigned int flags, u32 ext_filter_mask)
852 {
853 	struct ifinfomsg *ifm;
854 	struct nlmsghdr *nlh;
855 	struct rtnl_link_stats64 temp;
856 	const struct rtnl_link_stats64 *stats;
857 	struct nlattr *attr, *af_spec;
858 	struct rtnl_af_ops *af_ops;
859 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
860 
861 	ASSERT_RTNL();
862 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
863 	if (nlh == NULL)
864 		return -EMSGSIZE;
865 
866 	ifm = nlmsg_data(nlh);
867 	ifm->ifi_family = AF_UNSPEC;
868 	ifm->__ifi_pad = 0;
869 	ifm->ifi_type = dev->type;
870 	ifm->ifi_index = dev->ifindex;
871 	ifm->ifi_flags = dev_get_flags(dev);
872 	ifm->ifi_change = change;
873 
874 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
875 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
876 	    nla_put_u8(skb, IFLA_OPERSTATE,
877 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
878 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
879 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
880 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
881 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
882 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
883 #ifdef CONFIG_RPS
884 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
885 #endif
886 	    (dev->ifindex != dev->iflink &&
887 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
888 	    (upper_dev &&
889 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
890 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
891 	    (dev->qdisc &&
892 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
893 	    (dev->ifalias &&
894 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
895 		goto nla_put_failure;
896 
897 	if (1) {
898 		struct rtnl_link_ifmap map;
899 
900 		memset(&map, 0, sizeof(map));
901 		map.mem_start   = dev->mem_start;
902 		map.mem_end     = dev->mem_end;
903 		map.base_addr   = dev->base_addr;
904 		map.irq         = dev->irq;
905 		map.dma         = dev->dma;
906 		map.port        = dev->if_port;
907 
908 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
909 			goto nla_put_failure;
910 	}
911 
912 	if (dev->addr_len) {
913 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
914 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
915 			goto nla_put_failure;
916 	}
917 
918 	attr = nla_reserve(skb, IFLA_STATS,
919 			sizeof(struct rtnl_link_stats));
920 	if (attr == NULL)
921 		goto nla_put_failure;
922 
923 	stats = dev_get_stats(dev, &temp);
924 	copy_rtnl_link_stats(nla_data(attr), stats);
925 
926 	attr = nla_reserve(skb, IFLA_STATS64,
927 			sizeof(struct rtnl_link_stats64));
928 	if (attr == NULL)
929 		goto nla_put_failure;
930 	copy_rtnl_link_stats64(nla_data(attr), stats);
931 
932 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
933 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
934 		goto nla_put_failure;
935 
936 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
937 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
938 		int i;
939 
940 		struct nlattr *vfinfo, *vf;
941 		int num_vfs = dev_num_vf(dev->dev.parent);
942 
943 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
944 		if (!vfinfo)
945 			goto nla_put_failure;
946 		for (i = 0; i < num_vfs; i++) {
947 			struct ifla_vf_info ivi;
948 			struct ifla_vf_mac vf_mac;
949 			struct ifla_vf_vlan vf_vlan;
950 			struct ifla_vf_tx_rate vf_tx_rate;
951 			struct ifla_vf_spoofchk vf_spoofchk;
952 
953 			/*
954 			 * Not all SR-IOV capable drivers support the
955 			 * spoofcheck query.  Preset to -1 so the user
956 			 * space tool can detect that the driver didn't
957 			 * report anything.
958 			 */
959 			ivi.spoofchk = -1;
960 			memset(ivi.mac, 0, sizeof(ivi.mac));
961 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
962 				break;
963 			vf_mac.vf =
964 				vf_vlan.vf =
965 				vf_tx_rate.vf =
966 				vf_spoofchk.vf = ivi.vf;
967 
968 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
969 			vf_vlan.vlan = ivi.vlan;
970 			vf_vlan.qos = ivi.qos;
971 			vf_tx_rate.rate = ivi.tx_rate;
972 			vf_spoofchk.setting = ivi.spoofchk;
973 			vf = nla_nest_start(skb, IFLA_VF_INFO);
974 			if (!vf) {
975 				nla_nest_cancel(skb, vfinfo);
976 				goto nla_put_failure;
977 			}
978 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
979 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
980 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
981 				    &vf_tx_rate) ||
982 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
983 				    &vf_spoofchk))
984 				goto nla_put_failure;
985 			nla_nest_end(skb, vf);
986 		}
987 		nla_nest_end(skb, vfinfo);
988 	}
989 
990 	if (rtnl_port_fill(skb, dev))
991 		goto nla_put_failure;
992 
993 	if (dev->rtnl_link_ops) {
994 		if (rtnl_link_fill(skb, dev) < 0)
995 			goto nla_put_failure;
996 	}
997 
998 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
999 		goto nla_put_failure;
1000 
1001 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1002 		if (af_ops->fill_link_af) {
1003 			struct nlattr *af;
1004 			int err;
1005 
1006 			if (!(af = nla_nest_start(skb, af_ops->family)))
1007 				goto nla_put_failure;
1008 
1009 			err = af_ops->fill_link_af(skb, dev);
1010 
1011 			/*
1012 			 * Caller may return ENODATA to indicate that there
1013 			 * was no data to be dumped. This is not an error, it
1014 			 * means we should trim the attribute header and
1015 			 * continue.
1016 			 */
1017 			if (err == -ENODATA)
1018 				nla_nest_cancel(skb, af);
1019 			else if (err < 0)
1020 				goto nla_put_failure;
1021 
1022 			nla_nest_end(skb, af);
1023 		}
1024 	}
1025 
1026 	nla_nest_end(skb, af_spec);
1027 
1028 	return nlmsg_end(skb, nlh);
1029 
1030 nla_put_failure:
1031 	nlmsg_cancel(skb, nlh);
1032 	return -EMSGSIZE;
1033 }
1034 
rtnl_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)1035 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1036 {
1037 	struct net *net = sock_net(skb->sk);
1038 	int h, s_h;
1039 	int idx = 0, s_idx;
1040 	struct net_device *dev;
1041 	struct hlist_head *head;
1042 	struct nlattr *tb[IFLA_MAX+1];
1043 	u32 ext_filter_mask = 0;
1044 
1045 	s_h = cb->args[0];
1046 	s_idx = cb->args[1];
1047 
1048 	rcu_read_lock();
1049 	cb->seq = net->dev_base_seq;
1050 
1051 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1052 			ifla_policy) >= 0) {
1053 
1054 		if (tb[IFLA_EXT_MASK])
1055 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1056 	}
1057 
1058 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1059 		idx = 0;
1060 		head = &net->dev_index_head[h];
1061 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
1062 			if (idx < s_idx)
1063 				goto cont;
1064 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1065 					     NETLINK_CB(cb->skb).portid,
1066 					     cb->nlh->nlmsg_seq, 0,
1067 					     NLM_F_MULTI,
1068 					     ext_filter_mask) <= 0)
1069 				goto out;
1070 
1071 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1072 cont:
1073 			idx++;
1074 		}
1075 	}
1076 out:
1077 	rcu_read_unlock();
1078 	cb->args[1] = idx;
1079 	cb->args[0] = h;
1080 
1081 	return skb->len;
1082 }
1083 
1084 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1085 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1086 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1087 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1088 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1089 	[IFLA_MTU]		= { .type = NLA_U32 },
1090 	[IFLA_LINK]		= { .type = NLA_U32 },
1091 	[IFLA_MASTER]		= { .type = NLA_U32 },
1092 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1093 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1094 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1095 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1096 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1097 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1098 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1099 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1100 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1101 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1102 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1103 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1104 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1105 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1106 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1107 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1108 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1109 };
1110 EXPORT_SYMBOL(ifla_policy);
1111 
1112 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1113 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1114 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1115 };
1116 
1117 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1118 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1119 };
1120 
1121 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1122 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1123 				    .len = sizeof(struct ifla_vf_mac) },
1124 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1125 				    .len = sizeof(struct ifla_vf_vlan) },
1126 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1127 				    .len = sizeof(struct ifla_vf_tx_rate) },
1128 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1129 				    .len = sizeof(struct ifla_vf_spoofchk) },
1130 };
1131 
1132 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1133 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1134 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1135 				    .len = PORT_PROFILE_MAX },
1136 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1137 				    .len = sizeof(struct ifla_port_vsi)},
1138 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1139 				      .len = PORT_UUID_MAX },
1140 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1141 				    .len = PORT_UUID_MAX },
1142 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1143 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1144 };
1145 
rtnl_link_get_net(struct net * src_net,struct nlattr * tb[])1146 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1147 {
1148 	struct net *net;
1149 	/* Examine the link attributes and figure out which
1150 	 * network namespace we are talking about.
1151 	 */
1152 	if (tb[IFLA_NET_NS_PID])
1153 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1154 	else if (tb[IFLA_NET_NS_FD])
1155 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1156 	else
1157 		net = get_net(src_net);
1158 	return net;
1159 }
1160 EXPORT_SYMBOL(rtnl_link_get_net);
1161 
validate_linkmsg(struct net_device * dev,struct nlattr * tb[])1162 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1163 {
1164 	if (dev) {
1165 		if (tb[IFLA_ADDRESS] &&
1166 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1167 			return -EINVAL;
1168 
1169 		if (tb[IFLA_BROADCAST] &&
1170 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1171 			return -EINVAL;
1172 	}
1173 
1174 	if (tb[IFLA_AF_SPEC]) {
1175 		struct nlattr *af;
1176 		int rem, err;
1177 
1178 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1179 			const struct rtnl_af_ops *af_ops;
1180 
1181 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1182 				return -EAFNOSUPPORT;
1183 
1184 			if (!af_ops->set_link_af)
1185 				return -EOPNOTSUPP;
1186 
1187 			if (af_ops->validate_link_af) {
1188 				err = af_ops->validate_link_af(dev, af);
1189 				if (err < 0)
1190 					return err;
1191 			}
1192 		}
1193 	}
1194 
1195 	return 0;
1196 }
1197 
do_setvfinfo(struct net_device * dev,struct nlattr * attr)1198 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1199 {
1200 	int rem, err = -EINVAL;
1201 	struct nlattr *vf;
1202 	const struct net_device_ops *ops = dev->netdev_ops;
1203 
1204 	nla_for_each_nested(vf, attr, rem) {
1205 		switch (nla_type(vf)) {
1206 		case IFLA_VF_MAC: {
1207 			struct ifla_vf_mac *ivm;
1208 			ivm = nla_data(vf);
1209 			err = -EOPNOTSUPP;
1210 			if (ops->ndo_set_vf_mac)
1211 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1212 							  ivm->mac);
1213 			break;
1214 		}
1215 		case IFLA_VF_VLAN: {
1216 			struct ifla_vf_vlan *ivv;
1217 			ivv = nla_data(vf);
1218 			err = -EOPNOTSUPP;
1219 			if (ops->ndo_set_vf_vlan)
1220 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1221 							   ivv->vlan,
1222 							   ivv->qos);
1223 			break;
1224 		}
1225 		case IFLA_VF_TX_RATE: {
1226 			struct ifla_vf_tx_rate *ivt;
1227 			ivt = nla_data(vf);
1228 			err = -EOPNOTSUPP;
1229 			if (ops->ndo_set_vf_tx_rate)
1230 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1231 							      ivt->rate);
1232 			break;
1233 		}
1234 		case IFLA_VF_SPOOFCHK: {
1235 			struct ifla_vf_spoofchk *ivs;
1236 			ivs = nla_data(vf);
1237 			err = -EOPNOTSUPP;
1238 			if (ops->ndo_set_vf_spoofchk)
1239 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1240 							       ivs->setting);
1241 			break;
1242 		}
1243 		default:
1244 			err = -EINVAL;
1245 			break;
1246 		}
1247 		if (err)
1248 			break;
1249 	}
1250 	return err;
1251 }
1252 
do_set_master(struct net_device * dev,int ifindex)1253 static int do_set_master(struct net_device *dev, int ifindex)
1254 {
1255 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1256 	const struct net_device_ops *ops;
1257 	int err;
1258 
1259 	if (upper_dev) {
1260 		if (upper_dev->ifindex == ifindex)
1261 			return 0;
1262 		ops = upper_dev->netdev_ops;
1263 		if (ops->ndo_del_slave) {
1264 			err = ops->ndo_del_slave(upper_dev, dev);
1265 			if (err)
1266 				return err;
1267 		} else {
1268 			return -EOPNOTSUPP;
1269 		}
1270 	}
1271 
1272 	if (ifindex) {
1273 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1274 		if (!upper_dev)
1275 			return -EINVAL;
1276 		ops = upper_dev->netdev_ops;
1277 		if (ops->ndo_add_slave) {
1278 			err = ops->ndo_add_slave(upper_dev, dev);
1279 			if (err)
1280 				return err;
1281 		} else {
1282 			return -EOPNOTSUPP;
1283 		}
1284 	}
1285 	return 0;
1286 }
1287 
do_setlink(struct net_device * dev,struct ifinfomsg * ifm,struct nlattr ** tb,char * ifname,int modified)1288 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
1289 		      struct nlattr **tb, char *ifname, int modified)
1290 {
1291 	const struct net_device_ops *ops = dev->netdev_ops;
1292 	int err;
1293 
1294 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1295 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1296 		if (IS_ERR(net)) {
1297 			err = PTR_ERR(net);
1298 			goto errout;
1299 		}
1300 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1301 			err = -EPERM;
1302 			goto errout;
1303 		}
1304 		err = dev_change_net_namespace(dev, net, ifname);
1305 		put_net(net);
1306 		if (err)
1307 			goto errout;
1308 		modified = 1;
1309 	}
1310 
1311 	if (tb[IFLA_MAP]) {
1312 		struct rtnl_link_ifmap *u_map;
1313 		struct ifmap k_map;
1314 
1315 		if (!ops->ndo_set_config) {
1316 			err = -EOPNOTSUPP;
1317 			goto errout;
1318 		}
1319 
1320 		if (!netif_device_present(dev)) {
1321 			err = -ENODEV;
1322 			goto errout;
1323 		}
1324 
1325 		u_map = nla_data(tb[IFLA_MAP]);
1326 		k_map.mem_start = (unsigned long) u_map->mem_start;
1327 		k_map.mem_end = (unsigned long) u_map->mem_end;
1328 		k_map.base_addr = (unsigned short) u_map->base_addr;
1329 		k_map.irq = (unsigned char) u_map->irq;
1330 		k_map.dma = (unsigned char) u_map->dma;
1331 		k_map.port = (unsigned char) u_map->port;
1332 
1333 		err = ops->ndo_set_config(dev, &k_map);
1334 		if (err < 0)
1335 			goto errout;
1336 
1337 		modified = 1;
1338 	}
1339 
1340 	if (tb[IFLA_ADDRESS]) {
1341 		struct sockaddr *sa;
1342 		int len;
1343 
1344 		len = sizeof(sa_family_t) + dev->addr_len;
1345 		sa = kmalloc(len, GFP_KERNEL);
1346 		if (!sa) {
1347 			err = -ENOMEM;
1348 			goto errout;
1349 		}
1350 		sa->sa_family = dev->type;
1351 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1352 		       dev->addr_len);
1353 		err = dev_set_mac_address(dev, sa);
1354 		kfree(sa);
1355 		if (err)
1356 			goto errout;
1357 		modified = 1;
1358 	}
1359 
1360 	if (tb[IFLA_MTU]) {
1361 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1362 		if (err < 0)
1363 			goto errout;
1364 		modified = 1;
1365 	}
1366 
1367 	if (tb[IFLA_GROUP]) {
1368 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1369 		modified = 1;
1370 	}
1371 
1372 	/*
1373 	 * Interface selected by interface index but interface
1374 	 * name provided implies that a name change has been
1375 	 * requested.
1376 	 */
1377 	if (ifm->ifi_index > 0 && ifname[0]) {
1378 		err = dev_change_name(dev, ifname);
1379 		if (err < 0)
1380 			goto errout;
1381 		modified = 1;
1382 	}
1383 
1384 	if (tb[IFLA_IFALIAS]) {
1385 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1386 				    nla_len(tb[IFLA_IFALIAS]));
1387 		if (err < 0)
1388 			goto errout;
1389 		modified = 1;
1390 	}
1391 
1392 	if (tb[IFLA_BROADCAST]) {
1393 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1394 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1395 	}
1396 
1397 	if (ifm->ifi_flags || ifm->ifi_change) {
1398 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1399 		if (err < 0)
1400 			goto errout;
1401 	}
1402 
1403 	if (tb[IFLA_MASTER]) {
1404 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1405 		if (err)
1406 			goto errout;
1407 		modified = 1;
1408 	}
1409 
1410 	if (tb[IFLA_CARRIER]) {
1411 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1412 		if (err)
1413 			goto errout;
1414 		modified = 1;
1415 	}
1416 
1417 	if (tb[IFLA_TXQLEN])
1418 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1419 
1420 	if (tb[IFLA_OPERSTATE])
1421 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1422 
1423 	if (tb[IFLA_LINKMODE]) {
1424 		write_lock_bh(&dev_base_lock);
1425 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1426 		write_unlock_bh(&dev_base_lock);
1427 	}
1428 
1429 	if (tb[IFLA_VFINFO_LIST]) {
1430 		struct nlattr *attr;
1431 		int rem;
1432 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1433 			if (nla_type(attr) != IFLA_VF_INFO) {
1434 				err = -EINVAL;
1435 				goto errout;
1436 			}
1437 			err = do_setvfinfo(dev, attr);
1438 			if (err < 0)
1439 				goto errout;
1440 			modified = 1;
1441 		}
1442 	}
1443 	err = 0;
1444 
1445 	if (tb[IFLA_VF_PORTS]) {
1446 		struct nlattr *port[IFLA_PORT_MAX+1];
1447 		struct nlattr *attr;
1448 		int vf;
1449 		int rem;
1450 
1451 		err = -EOPNOTSUPP;
1452 		if (!ops->ndo_set_vf_port)
1453 			goto errout;
1454 
1455 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1456 			if (nla_type(attr) != IFLA_VF_PORT)
1457 				continue;
1458 			err = nla_parse_nested(port, IFLA_PORT_MAX,
1459 				attr, ifla_port_policy);
1460 			if (err < 0)
1461 				goto errout;
1462 			if (!port[IFLA_PORT_VF]) {
1463 				err = -EOPNOTSUPP;
1464 				goto errout;
1465 			}
1466 			vf = nla_get_u32(port[IFLA_PORT_VF]);
1467 			err = ops->ndo_set_vf_port(dev, vf, port);
1468 			if (err < 0)
1469 				goto errout;
1470 			modified = 1;
1471 		}
1472 	}
1473 	err = 0;
1474 
1475 	if (tb[IFLA_PORT_SELF]) {
1476 		struct nlattr *port[IFLA_PORT_MAX+1];
1477 
1478 		err = nla_parse_nested(port, IFLA_PORT_MAX,
1479 			tb[IFLA_PORT_SELF], ifla_port_policy);
1480 		if (err < 0)
1481 			goto errout;
1482 
1483 		err = -EOPNOTSUPP;
1484 		if (ops->ndo_set_vf_port)
1485 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1486 		if (err < 0)
1487 			goto errout;
1488 		modified = 1;
1489 	}
1490 
1491 	if (tb[IFLA_AF_SPEC]) {
1492 		struct nlattr *af;
1493 		int rem;
1494 
1495 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1496 			const struct rtnl_af_ops *af_ops;
1497 
1498 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1499 				BUG();
1500 
1501 			err = af_ops->set_link_af(dev, af);
1502 			if (err < 0)
1503 				goto errout;
1504 
1505 			modified = 1;
1506 		}
1507 	}
1508 	err = 0;
1509 
1510 errout:
1511 	if (err < 0 && modified)
1512 		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",
1513 				     dev->name);
1514 
1515 	return err;
1516 }
1517 
rtnl_setlink(struct sk_buff * skb,struct nlmsghdr * nlh)1518 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1519 {
1520 	struct net *net = sock_net(skb->sk);
1521 	struct ifinfomsg *ifm;
1522 	struct net_device *dev;
1523 	int err;
1524 	struct nlattr *tb[IFLA_MAX+1];
1525 	char ifname[IFNAMSIZ];
1526 
1527 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1528 	if (err < 0)
1529 		goto errout;
1530 
1531 	if (tb[IFLA_IFNAME])
1532 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1533 	else
1534 		ifname[0] = '\0';
1535 
1536 	err = -EINVAL;
1537 	ifm = nlmsg_data(nlh);
1538 	if (ifm->ifi_index > 0)
1539 		dev = __dev_get_by_index(net, ifm->ifi_index);
1540 	else if (tb[IFLA_IFNAME])
1541 		dev = __dev_get_by_name(net, ifname);
1542 	else
1543 		goto errout;
1544 
1545 	if (dev == NULL) {
1546 		err = -ENODEV;
1547 		goto errout;
1548 	}
1549 
1550 	err = validate_linkmsg(dev, tb);
1551 	if (err < 0)
1552 		goto errout;
1553 
1554 	err = do_setlink(dev, ifm, tb, ifname, 0);
1555 errout:
1556 	return err;
1557 }
1558 
rtnl_dellink(struct sk_buff * skb,struct nlmsghdr * nlh)1559 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
1560 {
1561 	struct net *net = sock_net(skb->sk);
1562 	const struct rtnl_link_ops *ops;
1563 	struct net_device *dev;
1564 	struct ifinfomsg *ifm;
1565 	char ifname[IFNAMSIZ];
1566 	struct nlattr *tb[IFLA_MAX+1];
1567 	int err;
1568 	LIST_HEAD(list_kill);
1569 
1570 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1571 	if (err < 0)
1572 		return err;
1573 
1574 	if (tb[IFLA_IFNAME])
1575 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1576 
1577 	ifm = nlmsg_data(nlh);
1578 	if (ifm->ifi_index > 0)
1579 		dev = __dev_get_by_index(net, ifm->ifi_index);
1580 	else if (tb[IFLA_IFNAME])
1581 		dev = __dev_get_by_name(net, ifname);
1582 	else
1583 		return -EINVAL;
1584 
1585 	if (!dev)
1586 		return -ENODEV;
1587 
1588 	ops = dev->rtnl_link_ops;
1589 	if (!ops)
1590 		return -EOPNOTSUPP;
1591 
1592 	ops->dellink(dev, &list_kill);
1593 	unregister_netdevice_many(&list_kill);
1594 	list_del(&list_kill);
1595 	return 0;
1596 }
1597 
rtnl_configure_link(struct net_device * dev,const struct ifinfomsg * ifm)1598 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1599 {
1600 	unsigned int old_flags;
1601 	int err;
1602 
1603 	old_flags = dev->flags;
1604 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1605 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1606 		if (err < 0)
1607 			return err;
1608 	}
1609 
1610 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1611 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1612 
1613 	__dev_notify_flags(dev, old_flags);
1614 	return 0;
1615 }
1616 EXPORT_SYMBOL(rtnl_configure_link);
1617 
rtnl_create_link(struct net * net,char * ifname,const struct rtnl_link_ops * ops,struct nlattr * tb[])1618 struct net_device *rtnl_create_link(struct net *net,
1619 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1620 {
1621 	int err;
1622 	struct net_device *dev;
1623 	unsigned int num_tx_queues = 1;
1624 	unsigned int num_rx_queues = 1;
1625 
1626 	if (tb[IFLA_NUM_TX_QUEUES])
1627 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1628 	else if (ops->get_num_tx_queues)
1629 		num_tx_queues = ops->get_num_tx_queues();
1630 
1631 	if (tb[IFLA_NUM_RX_QUEUES])
1632 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1633 	else if (ops->get_num_rx_queues)
1634 		num_rx_queues = ops->get_num_rx_queues();
1635 
1636 	err = -ENOMEM;
1637 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1638 			       num_tx_queues, num_rx_queues);
1639 	if (!dev)
1640 		goto err;
1641 
1642 	dev_net_set(dev, net);
1643 	dev->rtnl_link_ops = ops;
1644 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1645 
1646 	if (tb[IFLA_MTU])
1647 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1648 	if (tb[IFLA_ADDRESS]) {
1649 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1650 				nla_len(tb[IFLA_ADDRESS]));
1651 		dev->addr_assign_type = NET_ADDR_SET;
1652 	}
1653 	if (tb[IFLA_BROADCAST])
1654 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1655 				nla_len(tb[IFLA_BROADCAST]));
1656 	if (tb[IFLA_TXQLEN])
1657 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1658 	if (tb[IFLA_OPERSTATE])
1659 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1660 	if (tb[IFLA_LINKMODE])
1661 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1662 	if (tb[IFLA_GROUP])
1663 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1664 
1665 	return dev;
1666 
1667 err:
1668 	return ERR_PTR(err);
1669 }
1670 EXPORT_SYMBOL(rtnl_create_link);
1671 
rtnl_group_changelink(struct net * net,int group,struct ifinfomsg * ifm,struct nlattr ** tb)1672 static int rtnl_group_changelink(struct net *net, int group,
1673 		struct ifinfomsg *ifm,
1674 		struct nlattr **tb)
1675 {
1676 	struct net_device *dev;
1677 	int err;
1678 
1679 	for_each_netdev(net, dev) {
1680 		if (dev->group == group) {
1681 			err = do_setlink(dev, ifm, tb, NULL, 0);
1682 			if (err < 0)
1683 				return err;
1684 		}
1685 	}
1686 
1687 	return 0;
1688 }
1689 
rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh)1690 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1691 {
1692 	struct net *net = sock_net(skb->sk);
1693 	const struct rtnl_link_ops *ops;
1694 	struct net_device *dev;
1695 	struct ifinfomsg *ifm;
1696 	char kind[MODULE_NAME_LEN];
1697 	char ifname[IFNAMSIZ];
1698 	struct nlattr *tb[IFLA_MAX+1];
1699 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1700 	int err;
1701 
1702 #ifdef CONFIG_MODULES
1703 replay:
1704 #endif
1705 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1706 	if (err < 0)
1707 		return err;
1708 
1709 	if (tb[IFLA_IFNAME])
1710 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1711 	else
1712 		ifname[0] = '\0';
1713 
1714 	ifm = nlmsg_data(nlh);
1715 	if (ifm->ifi_index > 0)
1716 		dev = __dev_get_by_index(net, ifm->ifi_index);
1717 	else {
1718 		if (ifname[0])
1719 			dev = __dev_get_by_name(net, ifname);
1720 		else
1721 			dev = NULL;
1722 	}
1723 
1724 	err = validate_linkmsg(dev, tb);
1725 	if (err < 0)
1726 		return err;
1727 
1728 	if (tb[IFLA_LINKINFO]) {
1729 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1730 				       tb[IFLA_LINKINFO], ifla_info_policy);
1731 		if (err < 0)
1732 			return err;
1733 	} else
1734 		memset(linkinfo, 0, sizeof(linkinfo));
1735 
1736 	if (linkinfo[IFLA_INFO_KIND]) {
1737 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1738 		ops = rtnl_link_ops_get(kind);
1739 	} else {
1740 		kind[0] = '\0';
1741 		ops = NULL;
1742 	}
1743 
1744 	if (1) {
1745 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1746 		struct net *dest_net;
1747 
1748 		if (ops) {
1749 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1750 				err = nla_parse_nested(attr, ops->maxtype,
1751 						       linkinfo[IFLA_INFO_DATA],
1752 						       ops->policy);
1753 				if (err < 0)
1754 					return err;
1755 				data = attr;
1756 			}
1757 			if (ops->validate) {
1758 				err = ops->validate(tb, data);
1759 				if (err < 0)
1760 					return err;
1761 			}
1762 		}
1763 
1764 		if (dev) {
1765 			int modified = 0;
1766 
1767 			if (nlh->nlmsg_flags & NLM_F_EXCL)
1768 				return -EEXIST;
1769 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
1770 				return -EOPNOTSUPP;
1771 
1772 			if (linkinfo[IFLA_INFO_DATA]) {
1773 				if (!ops || ops != dev->rtnl_link_ops ||
1774 				    !ops->changelink)
1775 					return -EOPNOTSUPP;
1776 
1777 				err = ops->changelink(dev, tb, data);
1778 				if (err < 0)
1779 					return err;
1780 				modified = 1;
1781 			}
1782 
1783 			return do_setlink(dev, ifm, tb, ifname, modified);
1784 		}
1785 
1786 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1787 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1788 				return rtnl_group_changelink(net,
1789 						nla_get_u32(tb[IFLA_GROUP]),
1790 						ifm, tb);
1791 			return -ENODEV;
1792 		}
1793 
1794 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1795 			return -EOPNOTSUPP;
1796 
1797 		if (!ops) {
1798 #ifdef CONFIG_MODULES
1799 			if (kind[0]) {
1800 				__rtnl_unlock();
1801 				request_module("rtnl-link-%s", kind);
1802 				rtnl_lock();
1803 				ops = rtnl_link_ops_get(kind);
1804 				if (ops)
1805 					goto replay;
1806 			}
1807 #endif
1808 			return -EOPNOTSUPP;
1809 		}
1810 
1811 		if (!ifname[0])
1812 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1813 
1814 		dest_net = rtnl_link_get_net(net, tb);
1815 		if (IS_ERR(dest_net))
1816 			return PTR_ERR(dest_net);
1817 
1818 		dev = rtnl_create_link(dest_net, ifname, ops, tb);
1819 		if (IS_ERR(dev)) {
1820 			err = PTR_ERR(dev);
1821 			goto out;
1822 		}
1823 
1824 		dev->ifindex = ifm->ifi_index;
1825 
1826 		if (ops->newlink)
1827 			err = ops->newlink(net, dev, tb, data);
1828 		else
1829 			err = register_netdevice(dev);
1830 
1831 		if (err < 0 && !IS_ERR(dev))
1832 			free_netdev(dev);
1833 		if (err < 0)
1834 			goto out;
1835 
1836 		err = rtnl_configure_link(dev, ifm);
1837 		if (err < 0)
1838 			unregister_netdevice(dev);
1839 out:
1840 		put_net(dest_net);
1841 		return err;
1842 	}
1843 }
1844 
rtnl_getlink(struct sk_buff * skb,struct nlmsghdr * nlh)1845 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
1846 {
1847 	struct net *net = sock_net(skb->sk);
1848 	struct ifinfomsg *ifm;
1849 	char ifname[IFNAMSIZ];
1850 	struct nlattr *tb[IFLA_MAX+1];
1851 	struct net_device *dev = NULL;
1852 	struct sk_buff *nskb;
1853 	int err;
1854 	u32 ext_filter_mask = 0;
1855 
1856 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1857 	if (err < 0)
1858 		return err;
1859 
1860 	if (tb[IFLA_IFNAME])
1861 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1862 
1863 	if (tb[IFLA_EXT_MASK])
1864 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1865 
1866 	ifm = nlmsg_data(nlh);
1867 	if (ifm->ifi_index > 0)
1868 		dev = __dev_get_by_index(net, ifm->ifi_index);
1869 	else if (tb[IFLA_IFNAME])
1870 		dev = __dev_get_by_name(net, ifname);
1871 	else
1872 		return -EINVAL;
1873 
1874 	if (dev == NULL)
1875 		return -ENODEV;
1876 
1877 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1878 	if (nskb == NULL)
1879 		return -ENOBUFS;
1880 
1881 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
1882 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
1883 	if (err < 0) {
1884 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
1885 		WARN_ON(err == -EMSGSIZE);
1886 		kfree_skb(nskb);
1887 	} else
1888 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
1889 
1890 	return err;
1891 }
1892 
rtnl_calcit(struct sk_buff * skb,struct nlmsghdr * nlh)1893 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1894 {
1895 	struct net *net = sock_net(skb->sk);
1896 	struct net_device *dev;
1897 	struct nlattr *tb[IFLA_MAX+1];
1898 	u32 ext_filter_mask = 0;
1899 	u16 min_ifinfo_dump_size = 0;
1900 
1901 	if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1902 			ifla_policy) >= 0) {
1903 		if (tb[IFLA_EXT_MASK])
1904 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1905 	}
1906 
1907 	if (!ext_filter_mask)
1908 		return NLMSG_GOODSIZE;
1909 	/*
1910 	 * traverse the list of net devices and compute the minimum
1911 	 * buffer size based upon the filter mask.
1912 	 */
1913 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1914 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1915 					     if_nlmsg_size(dev,
1916 						           ext_filter_mask));
1917 	}
1918 
1919 	return min_ifinfo_dump_size;
1920 }
1921 
rtnl_dump_all(struct sk_buff * skb,struct netlink_callback * cb)1922 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1923 {
1924 	int idx;
1925 	int s_idx = cb->family;
1926 
1927 	if (s_idx == 0)
1928 		s_idx = 1;
1929 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1930 		int type = cb->nlh->nlmsg_type-RTM_BASE;
1931 		if (idx < s_idx || idx == PF_PACKET)
1932 			continue;
1933 		if (rtnl_msg_handlers[idx] == NULL ||
1934 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
1935 			continue;
1936 		if (idx > s_idx) {
1937 			memset(&cb->args[0], 0, sizeof(cb->args));
1938 			cb->prev_seq = 0;
1939 			cb->seq = 0;
1940 		}
1941 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1942 			break;
1943 	}
1944 	cb->family = idx;
1945 
1946 	return skb->len;
1947 }
1948 
rtmsg_ifinfo(int type,struct net_device * dev,unsigned int change)1949 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change)
1950 {
1951 	struct net *net = dev_net(dev);
1952 	struct sk_buff *skb;
1953 	int err = -ENOBUFS;
1954 	size_t if_info_size;
1955 
1956 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
1957 	if (skb == NULL)
1958 		goto errout;
1959 
1960 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1961 	if (err < 0) {
1962 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
1963 		WARN_ON(err == -EMSGSIZE);
1964 		kfree_skb(skb);
1965 		goto errout;
1966 	}
1967 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1968 	return;
1969 errout:
1970 	if (err < 0)
1971 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1972 }
1973 EXPORT_SYMBOL(rtmsg_ifinfo);
1974 
nlmsg_populate_fdb_fill(struct sk_buff * skb,struct net_device * dev,u8 * addr,u32 pid,u32 seq,int type,unsigned int flags)1975 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
1976 				   struct net_device *dev,
1977 				   u8 *addr, u32 pid, u32 seq,
1978 				   int type, unsigned int flags)
1979 {
1980 	struct nlmsghdr *nlh;
1981 	struct ndmsg *ndm;
1982 
1983 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), NLM_F_MULTI);
1984 	if (!nlh)
1985 		return -EMSGSIZE;
1986 
1987 	ndm = nlmsg_data(nlh);
1988 	ndm->ndm_family  = AF_BRIDGE;
1989 	ndm->ndm_pad1	 = 0;
1990 	ndm->ndm_pad2    = 0;
1991 	ndm->ndm_flags	 = flags;
1992 	ndm->ndm_type	 = 0;
1993 	ndm->ndm_ifindex = dev->ifindex;
1994 	ndm->ndm_state   = NUD_PERMANENT;
1995 
1996 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
1997 		goto nla_put_failure;
1998 
1999 	return nlmsg_end(skb, nlh);
2000 
2001 nla_put_failure:
2002 	nlmsg_cancel(skb, nlh);
2003 	return -EMSGSIZE;
2004 }
2005 
rtnl_fdb_nlmsg_size(void)2006 static inline size_t rtnl_fdb_nlmsg_size(void)
2007 {
2008 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2009 }
2010 
rtnl_fdb_notify(struct net_device * dev,u8 * addr,int type)2011 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2012 {
2013 	struct net *net = dev_net(dev);
2014 	struct sk_buff *skb;
2015 	int err = -ENOBUFS;
2016 
2017 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2018 	if (!skb)
2019 		goto errout;
2020 
2021 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF);
2022 	if (err < 0) {
2023 		kfree_skb(skb);
2024 		goto errout;
2025 	}
2026 
2027 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2028 	return;
2029 errout:
2030 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2031 }
2032 
2033 /**
2034  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2035  */
ndo_dflt_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 flags)2036 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2037 		     struct nlattr *tb[],
2038 		     struct net_device *dev,
2039 		     const unsigned char *addr,
2040 		     u16 flags)
2041 {
2042 	int err = -EINVAL;
2043 
2044 	/* If aging addresses are supported device will need to
2045 	 * implement its own handler for this.
2046 	 */
2047 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2048 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2049 		return err;
2050 	}
2051 
2052 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2053 		err = dev_uc_add_excl(dev, addr);
2054 	else if (is_multicast_ether_addr(addr))
2055 		err = dev_mc_add_excl(dev, addr);
2056 
2057 	/* Only return duplicate errors if NLM_F_EXCL is set */
2058 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2059 		err = 0;
2060 
2061 	return err;
2062 }
2063 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2064 
rtnl_fdb_add(struct sk_buff * skb,struct nlmsghdr * nlh)2065 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2066 {
2067 	struct net *net = sock_net(skb->sk);
2068 	struct ndmsg *ndm;
2069 	struct nlattr *tb[NDA_MAX+1];
2070 	struct net_device *dev;
2071 	u8 *addr;
2072 	int err;
2073 
2074 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2075 	if (err < 0)
2076 		return err;
2077 
2078 	ndm = nlmsg_data(nlh);
2079 	if (ndm->ndm_ifindex == 0) {
2080 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2081 		return -EINVAL;
2082 	}
2083 
2084 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2085 	if (dev == NULL) {
2086 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2087 		return -ENODEV;
2088 	}
2089 
2090 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2091 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2092 		return -EINVAL;
2093 	}
2094 
2095 	addr = nla_data(tb[NDA_LLADDR]);
2096 	if (is_zero_ether_addr(addr)) {
2097 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n");
2098 		return -EINVAL;
2099 	}
2100 
2101 	err = -EOPNOTSUPP;
2102 
2103 	/* Support fdb on master device the net/bridge default case */
2104 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2105 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2106 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2107 		const struct net_device_ops *ops = br_dev->netdev_ops;
2108 
2109 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
2110 		if (err)
2111 			goto out;
2112 		else
2113 			ndm->ndm_flags &= ~NTF_MASTER;
2114 	}
2115 
2116 	/* Embedded bridge, macvlan, and any other device support */
2117 	if ((ndm->ndm_flags & NTF_SELF)) {
2118 		if (dev->netdev_ops->ndo_fdb_add)
2119 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2120 							   nlh->nlmsg_flags);
2121 		else
2122 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
2123 					       nlh->nlmsg_flags);
2124 
2125 		if (!err) {
2126 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
2127 			ndm->ndm_flags &= ~NTF_SELF;
2128 		}
2129 	}
2130 out:
2131 	return err;
2132 }
2133 
2134 /**
2135  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2136  */
ndo_dflt_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr)2137 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2138 		     struct nlattr *tb[],
2139 		     struct net_device *dev,
2140 		     const unsigned char *addr)
2141 {
2142 	int err = -EOPNOTSUPP;
2143 
2144 	/* If aging addresses are supported device will need to
2145 	 * implement its own handler for this.
2146 	 */
2147 	if (ndm->ndm_state & NUD_PERMANENT) {
2148 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2149 		return -EINVAL;
2150 	}
2151 
2152 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2153 		err = dev_uc_del(dev, addr);
2154 	else if (is_multicast_ether_addr(addr))
2155 		err = dev_mc_del(dev, addr);
2156 	else
2157 		err = -EINVAL;
2158 
2159 	return err;
2160 }
2161 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2162 
rtnl_fdb_del(struct sk_buff * skb,struct nlmsghdr * nlh)2163 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2164 {
2165 	struct net *net = sock_net(skb->sk);
2166 	struct ndmsg *ndm;
2167 	struct nlattr *tb[NDA_MAX+1];
2168 	struct net_device *dev;
2169 	int err = -EINVAL;
2170 	__u8 *addr;
2171 
2172 	if (!capable(CAP_NET_ADMIN))
2173 		return -EPERM;
2174 
2175 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2176 	if (err < 0)
2177 		return err;
2178 
2179 	ndm = nlmsg_data(nlh);
2180 	if (ndm->ndm_ifindex == 0) {
2181 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2182 		return -EINVAL;
2183 	}
2184 
2185 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2186 	if (dev == NULL) {
2187 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2188 		return -ENODEV;
2189 	}
2190 
2191 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2192 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2193 		return -EINVAL;
2194 	}
2195 
2196 	addr = nla_data(tb[NDA_LLADDR]);
2197 	if (is_zero_ether_addr(addr)) {
2198 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ether address\n");
2199 		return -EINVAL;
2200 	}
2201 
2202 	err = -EOPNOTSUPP;
2203 
2204 	/* Support fdb on master device the net/bridge default case */
2205 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2206 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2207 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2208 		const struct net_device_ops *ops = br_dev->netdev_ops;
2209 
2210 		if (ops->ndo_fdb_del)
2211 			err = ops->ndo_fdb_del(ndm, tb, dev, addr);
2212 
2213 		if (err)
2214 			goto out;
2215 		else
2216 			ndm->ndm_flags &= ~NTF_MASTER;
2217 	}
2218 
2219 	/* Embedded bridge, macvlan, and any other device support */
2220 	if (ndm->ndm_flags & NTF_SELF) {
2221 		if (dev->netdev_ops->ndo_fdb_del)
2222 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2223 		else
2224 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
2225 
2226 		if (!err) {
2227 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
2228 			ndm->ndm_flags &= ~NTF_SELF;
2229 		}
2230 	}
2231 out:
2232 	return err;
2233 }
2234 
nlmsg_populate_fdb(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,int * idx,struct netdev_hw_addr_list * list)2235 static int nlmsg_populate_fdb(struct sk_buff *skb,
2236 			      struct netlink_callback *cb,
2237 			      struct net_device *dev,
2238 			      int *idx,
2239 			      struct netdev_hw_addr_list *list)
2240 {
2241 	struct netdev_hw_addr *ha;
2242 	int err;
2243 	u32 portid, seq;
2244 
2245 	portid = NETLINK_CB(cb->skb).portid;
2246 	seq = cb->nlh->nlmsg_seq;
2247 
2248 	list_for_each_entry(ha, &list->list, list) {
2249 		if (*idx < cb->args[0])
2250 			goto skip;
2251 
2252 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2253 					      portid, seq,
2254 					      RTM_NEWNEIGH, NTF_SELF);
2255 		if (err < 0)
2256 			return err;
2257 skip:
2258 		*idx += 1;
2259 	}
2260 	return 0;
2261 }
2262 
2263 /**
2264  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2265  * @nlh: netlink message header
2266  * @dev: netdevice
2267  *
2268  * Default netdevice operation to dump the existing unicast address list.
2269  * Returns number of addresses from list put in skb.
2270  */
ndo_dflt_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,int idx)2271 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2272 		      struct netlink_callback *cb,
2273 		      struct net_device *dev,
2274 		      int idx)
2275 {
2276 	int err;
2277 
2278 	netif_addr_lock_bh(dev);
2279 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2280 	if (err)
2281 		goto out;
2282 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2283 out:
2284 	netif_addr_unlock_bh(dev);
2285 	return idx;
2286 }
2287 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2288 
rtnl_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb)2289 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2290 {
2291 	int idx = 0;
2292 	struct net *net = sock_net(skb->sk);
2293 	struct net_device *dev;
2294 
2295 	rcu_read_lock();
2296 	for_each_netdev_rcu(net, dev) {
2297 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2298 			struct net_device *br_dev;
2299 			const struct net_device_ops *ops;
2300 
2301 			br_dev = netdev_master_upper_dev_get(dev);
2302 			ops = br_dev->netdev_ops;
2303 			if (ops->ndo_fdb_dump)
2304 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
2305 		}
2306 
2307 		if (dev->netdev_ops->ndo_fdb_dump)
2308 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2309 		else
2310 			idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
2311 	}
2312 	rcu_read_unlock();
2313 
2314 	cb->args[0] = idx;
2315 	return skb->len;
2316 }
2317 
ndo_dflt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u16 mode)2318 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2319 			    struct net_device *dev, u16 mode)
2320 {
2321 	struct nlmsghdr *nlh;
2322 	struct ifinfomsg *ifm;
2323 	struct nlattr *br_afspec;
2324 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2325 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2326 
2327 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2328 	if (nlh == NULL)
2329 		return -EMSGSIZE;
2330 
2331 	ifm = nlmsg_data(nlh);
2332 	ifm->ifi_family = AF_BRIDGE;
2333 	ifm->__ifi_pad = 0;
2334 	ifm->ifi_type = dev->type;
2335 	ifm->ifi_index = dev->ifindex;
2336 	ifm->ifi_flags = dev_get_flags(dev);
2337 	ifm->ifi_change = 0;
2338 
2339 
2340 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2341 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2342 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2343 	    (br_dev &&
2344 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2345 	    (dev->addr_len &&
2346 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2347 	    (dev->ifindex != dev->iflink &&
2348 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2349 		goto nla_put_failure;
2350 
2351 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2352 	if (!br_afspec)
2353 		goto nla_put_failure;
2354 
2355 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2356 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2357 		nla_nest_cancel(skb, br_afspec);
2358 		goto nla_put_failure;
2359 	}
2360 	nla_nest_end(skb, br_afspec);
2361 
2362 	return nlmsg_end(skb, nlh);
2363 nla_put_failure:
2364 	nlmsg_cancel(skb, nlh);
2365 	return -EMSGSIZE;
2366 }
2367 EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2368 
rtnl_bridge_getlink(struct sk_buff * skb,struct netlink_callback * cb)2369 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2370 {
2371 	struct net *net = sock_net(skb->sk);
2372 	struct net_device *dev;
2373 	int idx = 0;
2374 	u32 portid = NETLINK_CB(cb->skb).portid;
2375 	u32 seq = cb->nlh->nlmsg_seq;
2376 	struct nlattr *extfilt;
2377 	u32 filter_mask = 0;
2378 
2379 	extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct rtgenmsg),
2380 				  IFLA_EXT_MASK);
2381 	if (extfilt)
2382 		filter_mask = nla_get_u32(extfilt);
2383 
2384 	rcu_read_lock();
2385 	for_each_netdev_rcu(net, dev) {
2386 		const struct net_device_ops *ops = dev->netdev_ops;
2387 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2388 
2389 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2390 			if (idx >= cb->args[0] &&
2391 			    br_dev->netdev_ops->ndo_bridge_getlink(
2392 				    skb, portid, seq, dev, filter_mask) < 0)
2393 				break;
2394 			idx++;
2395 		}
2396 
2397 		if (ops->ndo_bridge_getlink) {
2398 			if (idx >= cb->args[0] &&
2399 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
2400 						    filter_mask) < 0)
2401 				break;
2402 			idx++;
2403 		}
2404 	}
2405 	rcu_read_unlock();
2406 	cb->args[0] = idx;
2407 
2408 	return skb->len;
2409 }
2410 
bridge_nlmsg_size(void)2411 static inline size_t bridge_nlmsg_size(void)
2412 {
2413 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2414 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
2415 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
2416 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
2417 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
2418 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
2419 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
2420 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
2421 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
2422 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
2423 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
2424 }
2425 
rtnl_bridge_notify(struct net_device * dev,u16 flags)2426 static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2427 {
2428 	struct net *net = dev_net(dev);
2429 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2430 	struct sk_buff *skb;
2431 	int err = -EOPNOTSUPP;
2432 
2433 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2434 	if (!skb) {
2435 		err = -ENOMEM;
2436 		goto errout;
2437 	}
2438 
2439 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2440 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2441 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2442 		if (err < 0)
2443 			goto errout;
2444 	}
2445 
2446 	if ((flags & BRIDGE_FLAGS_SELF) &&
2447 	    dev->netdev_ops->ndo_bridge_getlink) {
2448 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2449 		if (err < 0)
2450 			goto errout;
2451 	}
2452 
2453 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2454 	return 0;
2455 errout:
2456 	WARN_ON(err == -EMSGSIZE);
2457 	kfree_skb(skb);
2458 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2459 	return err;
2460 }
2461 
rtnl_bridge_setlink(struct sk_buff * skb,struct nlmsghdr * nlh)2462 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2463 {
2464 	struct net *net = sock_net(skb->sk);
2465 	struct ifinfomsg *ifm;
2466 	struct net_device *dev;
2467 	struct nlattr *br_spec, *attr = NULL;
2468 	int rem, err = -EOPNOTSUPP;
2469 	u16 oflags, flags = 0;
2470 	bool have_flags = false;
2471 
2472 	if (nlmsg_len(nlh) < sizeof(*ifm))
2473 		return -EINVAL;
2474 
2475 	ifm = nlmsg_data(nlh);
2476 	if (ifm->ifi_family != AF_BRIDGE)
2477 		return -EPFNOSUPPORT;
2478 
2479 	dev = __dev_get_by_index(net, ifm->ifi_index);
2480 	if (!dev) {
2481 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2482 		return -ENODEV;
2483 	}
2484 
2485 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2486 	if (br_spec) {
2487 		nla_for_each_nested(attr, br_spec, rem) {
2488 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2489 				have_flags = true;
2490 				flags = nla_get_u16(attr);
2491 				break;
2492 			}
2493 		}
2494 	}
2495 
2496 	oflags = flags;
2497 
2498 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2499 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2500 
2501 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2502 			err = -EOPNOTSUPP;
2503 			goto out;
2504 		}
2505 
2506 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2507 		if (err)
2508 			goto out;
2509 
2510 		flags &= ~BRIDGE_FLAGS_MASTER;
2511 	}
2512 
2513 	if ((flags & BRIDGE_FLAGS_SELF)) {
2514 		if (!dev->netdev_ops->ndo_bridge_setlink)
2515 			err = -EOPNOTSUPP;
2516 		else
2517 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2518 
2519 		if (!err)
2520 			flags &= ~BRIDGE_FLAGS_SELF;
2521 	}
2522 
2523 	if (have_flags)
2524 		memcpy(nla_data(attr), &flags, sizeof(flags));
2525 	/* Generate event to notify upper layer of bridge change */
2526 	if (!err)
2527 		err = rtnl_bridge_notify(dev, oflags);
2528 out:
2529 	return err;
2530 }
2531 
rtnl_bridge_dellink(struct sk_buff * skb,struct nlmsghdr * nlh)2532 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2533 {
2534 	struct net *net = sock_net(skb->sk);
2535 	struct ifinfomsg *ifm;
2536 	struct net_device *dev;
2537 	struct nlattr *br_spec, *attr = NULL;
2538 	int rem, err = -EOPNOTSUPP;
2539 	u16 oflags, flags = 0;
2540 	bool have_flags = false;
2541 
2542 	if (nlmsg_len(nlh) < sizeof(*ifm))
2543 		return -EINVAL;
2544 
2545 	ifm = nlmsg_data(nlh);
2546 	if (ifm->ifi_family != AF_BRIDGE)
2547 		return -EPFNOSUPPORT;
2548 
2549 	dev = __dev_get_by_index(net, ifm->ifi_index);
2550 	if (!dev) {
2551 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2552 		return -ENODEV;
2553 	}
2554 
2555 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2556 	if (br_spec) {
2557 		nla_for_each_nested(attr, br_spec, rem) {
2558 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2559 				have_flags = true;
2560 				flags = nla_get_u16(attr);
2561 				break;
2562 			}
2563 		}
2564 	}
2565 
2566 	oflags = flags;
2567 
2568 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2569 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2570 
2571 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2572 			err = -EOPNOTSUPP;
2573 			goto out;
2574 		}
2575 
2576 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2577 		if (err)
2578 			goto out;
2579 
2580 		flags &= ~BRIDGE_FLAGS_MASTER;
2581 	}
2582 
2583 	if ((flags & BRIDGE_FLAGS_SELF)) {
2584 		if (!dev->netdev_ops->ndo_bridge_dellink)
2585 			err = -EOPNOTSUPP;
2586 		else
2587 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2588 
2589 		if (!err)
2590 			flags &= ~BRIDGE_FLAGS_SELF;
2591 	}
2592 
2593 	if (have_flags)
2594 		memcpy(nla_data(attr), &flags, sizeof(flags));
2595 	/* Generate event to notify upper layer of bridge change */
2596 	if (!err)
2597 		err = rtnl_bridge_notify(dev, oflags);
2598 out:
2599 	return err;
2600 }
2601 
2602 /* Process one rtnetlink message. */
2603 
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh)2604 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2605 {
2606 	struct net *net = sock_net(skb->sk);
2607 	rtnl_doit_func doit;
2608 	int sz_idx, kind;
2609 	int family;
2610 	int type;
2611 	int err;
2612 
2613 	type = nlh->nlmsg_type;
2614 	if (type > RTM_MAX)
2615 		return -EOPNOTSUPP;
2616 
2617 	type -= RTM_BASE;
2618 
2619 	/* All the messages must have at least 1 byte length */
2620 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
2621 		return 0;
2622 
2623 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
2624 	sz_idx = type>>2;
2625 	kind = type&3;
2626 
2627 	if (kind != 2 && !ns_capable(net->user_ns, CAP_NET_ADMIN))
2628 		return -EPERM;
2629 
2630 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
2631 		struct sock *rtnl;
2632 		rtnl_dumpit_func dumpit;
2633 		rtnl_calcit_func calcit;
2634 		u16 min_dump_alloc = 0;
2635 
2636 		dumpit = rtnl_get_dumpit(family, type);
2637 		if (dumpit == NULL)
2638 			return -EOPNOTSUPP;
2639 		calcit = rtnl_get_calcit(family, type);
2640 		if (calcit)
2641 			min_dump_alloc = calcit(skb, nlh);
2642 
2643 		__rtnl_unlock();
2644 		rtnl = net->rtnl;
2645 		{
2646 			struct netlink_dump_control c = {
2647 				.dump		= dumpit,
2648 				.min_dump_alloc	= min_dump_alloc,
2649 			};
2650 			err = netlink_dump_start(rtnl, skb, nlh, &c);
2651 		}
2652 		rtnl_lock();
2653 		return err;
2654 	}
2655 
2656 	doit = rtnl_get_doit(family, type);
2657 	if (doit == NULL)
2658 		return -EOPNOTSUPP;
2659 
2660 	return doit(skb, nlh);
2661 }
2662 
rtnetlink_rcv(struct sk_buff * skb)2663 static void rtnetlink_rcv(struct sk_buff *skb)
2664 {
2665 	rtnl_lock();
2666 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2667 	rtnl_unlock();
2668 }
2669 
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)2670 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2671 {
2672 	struct net_device *dev = ptr;
2673 
2674 	switch (event) {
2675 	case NETDEV_UP:
2676 	case NETDEV_DOWN:
2677 	case NETDEV_PRE_UP:
2678 	case NETDEV_POST_INIT:
2679 	case NETDEV_REGISTER:
2680 	case NETDEV_CHANGE:
2681 	case NETDEV_PRE_TYPE_CHANGE:
2682 	case NETDEV_GOING_DOWN:
2683 	case NETDEV_UNREGISTER:
2684 	case NETDEV_UNREGISTER_FINAL:
2685 	case NETDEV_RELEASE:
2686 	case NETDEV_JOIN:
2687 		break;
2688 	default:
2689 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2690 		break;
2691 	}
2692 	return NOTIFY_DONE;
2693 }
2694 
2695 static struct notifier_block rtnetlink_dev_notifier = {
2696 	.notifier_call	= rtnetlink_event,
2697 };
2698 
2699 
rtnetlink_net_init(struct net * net)2700 static int __net_init rtnetlink_net_init(struct net *net)
2701 {
2702 	struct sock *sk;
2703 	struct netlink_kernel_cfg cfg = {
2704 		.groups		= RTNLGRP_MAX,
2705 		.input		= rtnetlink_rcv,
2706 		.cb_mutex	= &rtnl_mutex,
2707 		.flags		= NL_CFG_F_NONROOT_RECV,
2708 	};
2709 
2710 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
2711 	if (!sk)
2712 		return -ENOMEM;
2713 	net->rtnl = sk;
2714 	return 0;
2715 }
2716 
rtnetlink_net_exit(struct net * net)2717 static void __net_exit rtnetlink_net_exit(struct net *net)
2718 {
2719 	netlink_kernel_release(net->rtnl);
2720 	net->rtnl = NULL;
2721 }
2722 
2723 static struct pernet_operations rtnetlink_net_ops = {
2724 	.init = rtnetlink_net_init,
2725 	.exit = rtnetlink_net_exit,
2726 };
2727 
rtnetlink_init(void)2728 void __init rtnetlink_init(void)
2729 {
2730 	if (register_pernet_subsys(&rtnetlink_net_ops))
2731 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
2732 
2733 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2734 
2735 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2736 		      rtnl_dump_ifinfo, rtnl_calcit);
2737 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2738 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2739 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2740 
2741 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2742 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
2743 
2744 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
2745 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
2746 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2747 
2748 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2749 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
2750 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
2751 }
2752 
2753