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/config.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/major.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <linux/fcntl.h>
31 #include <linux/mm.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/capability.h>
35 #include <linux/skbuff.h>
36 #include <linux/init.h>
37 
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/string.h>
41 
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <net/ip.h>
45 #include <net/protocol.h>
46 #include <net/arp.h>
47 #include <net/route.h>
48 #include <net/udp.h>
49 #include <net/sock.h>
50 #include <net/pkt_sched.h>
51 
52 DECLARE_MUTEX(rtnl_sem);
53 
rtnl_lock(void)54 void rtnl_lock(void)
55 {
56 	rtnl_shlock();
57 	rtnl_exlock();
58 }
59 
rtnl_unlock(void)60 void rtnl_unlock(void)
61 {
62 	rtnl_exunlock();
63 	rtnl_shunlock();
64 }
65 
rtattr_parse(struct rtattr * tb[],int maxattr,struct rtattr * rta,int len)66 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
67 {
68 	memset(tb, 0, sizeof(struct rtattr*)*maxattr);
69 
70 	while (RTA_OK(rta, len)) {
71 		unsigned flavor = rta->rta_type;
72 		if (flavor && flavor <= maxattr)
73 			tb[flavor-1] = rta;
74 		rta = RTA_NEXT(rta, len);
75 	}
76 	return 0;
77 }
78 
79 struct sock *rtnl;
80 
81 struct rtnetlink_link * rtnetlink_links[NPROTO];
82 
83 static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
84 {
85 	NLMSG_LENGTH(sizeof(struct ifinfomsg)),
86 	NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
87 	NLMSG_LENGTH(sizeof(struct rtmsg)),
88 	NLMSG_LENGTH(sizeof(struct ndmsg)),
89 	NLMSG_LENGTH(sizeof(struct rtmsg)),
90 	NLMSG_LENGTH(sizeof(struct tcmsg)),
91 	NLMSG_LENGTH(sizeof(struct tcmsg)),
92 	NLMSG_LENGTH(sizeof(struct tcmsg))
93 };
94 
95 static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
96 {
97 	IFLA_MAX,
98 	IFA_MAX,
99 	RTA_MAX,
100 	NDA_MAX,
101 	RTA_MAX,
102 	TCA_MAX,
103 	TCA_MAX,
104 	TCA_MAX
105 };
106 
__rta_fill(struct sk_buff * skb,int attrtype,int attrlen,const void * data)107 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
108 {
109 	struct rtattr *rta;
110 	int size = RTA_LENGTH(attrlen);
111 
112 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
113 	rta->rta_type = attrtype;
114 	rta->rta_len = size;
115 	memcpy(RTA_DATA(rta), data, attrlen);
116 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
117 }
118 
rtnetlink_send(struct sk_buff * skb,u32 pid,unsigned group,int echo)119 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
120 {
121 	int err = 0;
122 
123 	NETLINK_CB(skb).dst_groups = group;
124 	if (echo)
125 		atomic_inc(&skb->users);
126 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
127 	if (echo)
128 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
129 	return err;
130 }
131 
rtnetlink_put_metrics(struct sk_buff * skb,unsigned * metrics)132 int rtnetlink_put_metrics(struct sk_buff *skb, unsigned *metrics)
133 {
134 	struct rtattr *mx = (struct rtattr*)skb->tail;
135 	int i;
136 
137 	RTA_PUT(skb, RTA_METRICS, 0, NULL);
138 	for (i=0; i<RTAX_MAX; i++) {
139 		if (metrics[i])
140 			RTA_PUT(skb, i+1, sizeof(unsigned), metrics+i);
141 	}
142 	mx->rta_len = skb->tail - (u8*)mx;
143 	if (mx->rta_len == RTA_LENGTH(0))
144 		skb_trim(skb, (u8*)mx - skb->data);
145 	return 0;
146 
147 rtattr_failure:
148 	skb_trim(skb, (u8*)mx - skb->data);
149 	return -1;
150 }
151 
152 
rtnetlink_fill_ifinfo(struct sk_buff * skb,struct net_device * dev,int type,u32 pid,u32 seq,u32 change)153 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
154 				 int type, u32 pid, u32 seq, u32 change)
155 {
156 	struct ifinfomsg *r;
157 	struct nlmsghdr  *nlh;
158 	unsigned char	 *b = skb->tail;
159 
160 	nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
161 	if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
162 	r = NLMSG_DATA(nlh);
163 	r->ifi_family = AF_UNSPEC;
164 	r->__ifi_pad = 0;
165 	r->ifi_type = dev->type;
166 	r->ifi_index = dev->ifindex;
167 	r->ifi_flags = dev->flags;
168 	r->ifi_change = change;
169 
170 	if (!netif_running(dev) || !netif_carrier_ok(dev))
171 		r->ifi_flags &= ~IFF_RUNNING;
172 	else
173 		r->ifi_flags |= IFF_RUNNING;
174 
175 	RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
176 	if (dev->addr_len) {
177 		RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
178 		RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
179 	}
180 	if (1) {
181 		unsigned mtu = dev->mtu;
182 		RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
183 	}
184 	if (dev->ifindex != dev->iflink)
185 		RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
186 	if (dev->qdisc_sleeping)
187 		RTA_PUT(skb, IFLA_QDISC,
188 			strlen(dev->qdisc_sleeping->ops->id) + 1,
189 			dev->qdisc_sleeping->ops->id);
190 	if (dev->master)
191 		RTA_PUT(skb, IFLA_MASTER, sizeof(int), &dev->master->ifindex);
192 	if (dev->get_stats) {
193 		struct net_device_stats *stats = dev->get_stats(dev);
194 		if (stats)
195 			RTA_PUT(skb, IFLA_STATS, sizeof(*stats), stats);
196 	}
197 	nlh->nlmsg_len = skb->tail - b;
198 	return skb->len;
199 
200 nlmsg_failure:
201 rtattr_failure:
202 	skb_trim(skb, b - skb->data);
203 	return -1;
204 }
205 
rtnetlink_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)206 int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
207 {
208 	int idx;
209 	int s_idx = cb->args[0];
210 	struct net_device *dev;
211 
212 	read_lock(&dev_base_lock);
213 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
214 		if (idx < s_idx)
215 			continue;
216 		if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
217 			break;
218 	}
219 	read_unlock(&dev_base_lock);
220 	cb->args[0] = idx;
221 
222 	return skb->len;
223 }
224 
rtnetlink_dump_all(struct sk_buff * skb,struct netlink_callback * cb)225 int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
226 {
227 	int idx;
228 	int s_idx = cb->family;
229 
230 	if (s_idx == 0)
231 		s_idx = 1;
232 	for (idx=1; idx<NPROTO; idx++) {
233 		int type = cb->nlh->nlmsg_type-RTM_BASE;
234 		if (idx < s_idx || idx == PF_PACKET)
235 			continue;
236 		if (rtnetlink_links[idx] == NULL ||
237 		    rtnetlink_links[idx][type].dumpit == NULL)
238 			continue;
239 		if (idx > s_idx)
240 			memset(&cb->args[0], 0, sizeof(cb->args));
241 		if (rtnetlink_links[idx][type].dumpit(skb, cb))
242 			break;
243 	}
244 	cb->family = idx;
245 
246 	return skb->len;
247 }
248 
rtmsg_ifinfo(int type,struct net_device * dev,unsigned change)249 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
250 {
251 	struct sk_buff *skb;
252 	int size = NLMSG_GOODSIZE;
253 
254 	skb = alloc_skb(size, GFP_KERNEL);
255 	if (!skb)
256 		return;
257 
258 	if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
259 		kfree_skb(skb);
260 		return;
261 	}
262 	NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
263 	netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
264 }
265 
rtnetlink_done(struct netlink_callback * cb)266 static int rtnetlink_done(struct netlink_callback *cb)
267 {
268 	return 0;
269 }
270 
271 /* Process one rtnetlink message. */
272 
273 static __inline__ int
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh,int * errp)274 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
275 {
276 	struct rtnetlink_link *link;
277 	struct rtnetlink_link *link_tab;
278 	struct rtattr	*rta[RTATTR_MAX];
279 
280 	int exclusive = 0;
281 	int sz_idx, kind;
282 	int min_len;
283 	int family;
284 	int type;
285 	int err;
286 
287 	/* Only requests are handled by kernel now */
288 	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
289 		return 0;
290 
291 	type = nlh->nlmsg_type;
292 
293 	/* A control message: ignore them */
294 	if (type < RTM_BASE)
295 		return 0;
296 
297 	/* Unknown message: reply with EINVAL */
298 	if (type > RTM_MAX)
299 		goto err_inval;
300 
301 	type -= RTM_BASE;
302 
303 	/* All the messages must have at least 1 byte length */
304 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
305 		return 0;
306 
307 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
308 	if (family >= NPROTO) {
309 		*errp = -EAFNOSUPPORT;
310 		return -1;
311 	}
312 
313 	link_tab = rtnetlink_links[family];
314 	if (link_tab == NULL)
315 		link_tab = rtnetlink_links[PF_UNSPEC];
316 	link = &link_tab[type];
317 
318 	sz_idx = type>>2;
319 	kind = type&3;
320 
321 	if (kind != 2 && !cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) {
322 		*errp = -EPERM;
323 		return -1;
324 	}
325 
326 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
327 		u32 rlen;
328 
329 		if (link->dumpit == NULL)
330 			link = &(rtnetlink_links[PF_UNSPEC][type]);
331 
332 		if (link->dumpit == NULL)
333 			goto err_inval;
334 
335 		if ((*errp = netlink_dump_start(rtnl, skb, nlh,
336 						link->dumpit,
337 						rtnetlink_done)) != 0) {
338 			return -1;
339 		}
340 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
341 		if (rlen > skb->len)
342 			rlen = skb->len;
343 		skb_pull(skb, rlen);
344 		return -1;
345 	}
346 
347 	if (kind != 2) {
348 		if (rtnl_exlock_nowait()) {
349 			*errp = 0;
350 			return -1;
351 		}
352 		exclusive = 1;
353 	}
354 
355 	memset(&rta, 0, sizeof(rta));
356 
357 	min_len = rtm_min[sz_idx];
358 	if (nlh->nlmsg_len < min_len)
359 		goto err_inval;
360 
361 	if (nlh->nlmsg_len > min_len) {
362 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
363 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
364 
365 		while (RTA_OK(attr, attrlen)) {
366 			unsigned flavor = attr->rta_type;
367 			if (flavor) {
368 				if (flavor > rta_max[sz_idx])
369 					goto err_inval;
370 				rta[flavor-1] = attr;
371 			}
372 			attr = RTA_NEXT(attr, attrlen);
373 		}
374 	}
375 
376 	if (link->doit == NULL)
377 		link = &(rtnetlink_links[PF_UNSPEC][type]);
378 	if (link->doit == NULL)
379 		goto err_inval;
380 	err = link->doit(skb, nlh, (void *)&rta);
381 
382 	if (exclusive)
383 		rtnl_exunlock();
384 	*errp = err;
385 	return err;
386 
387 err_inval:
388 	if (exclusive)
389 		rtnl_exunlock();
390 	*errp = -EINVAL;
391 	return -1;
392 }
393 
394 /*
395  * Process one packet of messages.
396  * Malformed skbs with wrong lengths of messages are discarded silently.
397  */
398 
rtnetlink_rcv_skb(struct sk_buff * skb)399 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
400 {
401 	int err;
402 	struct nlmsghdr * nlh;
403 
404 	while (skb->len >= NLMSG_SPACE(0)) {
405 		u32 rlen;
406 
407 		nlh = (struct nlmsghdr *)skb->data;
408 		if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
409 			return 0;
410 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
411 		if (rlen > skb->len)
412 			rlen = skb->len;
413 		if (rtnetlink_rcv_msg(skb, nlh, &err)) {
414 			/* Not error, but we must interrupt processing here:
415 			 *   Note, that in this case we do not pull message
416 			 *   from skb, it will be processed later.
417 			 */
418 			if (err == 0)
419 				return -1;
420 			netlink_ack(skb, nlh, err);
421 		} else if (nlh->nlmsg_flags&NLM_F_ACK)
422 			netlink_ack(skb, nlh, 0);
423 		skb_pull(skb, rlen);
424 	}
425 
426 	return 0;
427 }
428 
429 /*
430  *  rtnetlink input queue processing routine:
431  *	- try to acquire shared lock. If it is failed, defer processing.
432  *	- feed skbs to rtnetlink_rcv_skb, until it refuse a message,
433  *	  that will occur, when a dump started and/or acquisition of
434  *	  exclusive lock failed.
435  */
436 
rtnetlink_rcv(struct sock * sk,int len)437 static void rtnetlink_rcv(struct sock *sk, int len)
438 {
439 	do {
440 		struct sk_buff *skb;
441 
442 		if (rtnl_shlock_nowait())
443 			return;
444 
445 		while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
446 			if (rtnetlink_rcv_skb(skb)) {
447 				if (skb->len)
448 					skb_queue_head(&sk->receive_queue, skb);
449 				else
450 					kfree_skb(skb);
451 				break;
452 			}
453 			kfree_skb(skb);
454 		}
455 
456 		up(&rtnl_sem);
457 	} while (rtnl && rtnl->receive_queue.qlen);
458 }
459 
460 static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
461 {
462 	{ NULL,			NULL,			},
463 	{ NULL,			NULL,			},
464 	{ NULL,			rtnetlink_dump_ifinfo,	},
465 	{ NULL,			NULL,			},
466 
467 	{ NULL,			NULL,			},
468 	{ NULL,			NULL,			},
469 	{ NULL,			rtnetlink_dump_all,	},
470 	{ NULL,			NULL,			},
471 
472 	{ NULL,			NULL,			},
473 	{ NULL,			NULL,			},
474 	{ NULL,			rtnetlink_dump_all,	},
475 	{ NULL,			NULL,			},
476 
477 	{ neigh_add,		NULL,			},
478 	{ neigh_delete,		NULL,			},
479 	{ NULL,			neigh_dump_info,	},
480 	{ NULL,			NULL,			},
481 
482 	{ NULL,			NULL,			},
483 	{ NULL,			NULL,			},
484 	{ NULL,			NULL,			},
485 	{ NULL,			NULL,			},
486 };
487 
488 
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)489 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
490 {
491 	struct net_device *dev = ptr;
492 	switch (event) {
493 	case NETDEV_UNREGISTER:
494 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
495 		break;
496 	case NETDEV_REGISTER:
497 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
498 		break;
499 	case NETDEV_UP:
500 	case NETDEV_DOWN:
501 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
502 		break;
503 	case NETDEV_CHANGE:
504 	case NETDEV_GOING_DOWN:
505 		break;
506 	default:
507 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
508 		break;
509 	}
510 	return NOTIFY_DONE;
511 }
512 
513 struct notifier_block rtnetlink_dev_notifier = {
514 	rtnetlink_event,
515 	NULL,
516 	0
517 };
518 
519 
rtnetlink_init(void)520 void __init rtnetlink_init(void)
521 {
522 #ifdef RTNL_DEBUG
523 	printk("Initializing RT netlink socket\n");
524 #endif
525 	rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
526 	if (rtnl == NULL)
527 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
528 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
529 	register_netdevice_notifier(&rtnetlink_dev_notifier);
530 	rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
531 	rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
532 }
533