rtnetlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/socket.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.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 <asm/uaccess.h>
  38. #include <asm/system.h>
  39. #include <asm/string.h>
  40. #include <linux/inet.h>
  41. #include <linux/netdevice.h>
  42. #include <net/ip.h>
  43. #include <net/protocol.h>
  44. #include <net/arp.h>
  45. #include <net/route.h>
  46. #include <net/udp.h>
  47. #include <net/sock.h>
  48. #include <net/pkt_sched.h>
  49. #include <net/netlink.h>
  50. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  51. #include <linux/wireless.h>
  52. #include <net/iw_handler.h>
  53. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  54. static DEFINE_MUTEX(rtnl_mutex);
  55. void rtnl_lock(void)
  56. {
  57. mutex_lock(&rtnl_mutex);
  58. }
  59. void __rtnl_unlock(void)
  60. {
  61. mutex_unlock(&rtnl_mutex);
  62. }
  63. void rtnl_unlock(void)
  64. {
  65. mutex_unlock(&rtnl_mutex);
  66. if (rtnl && rtnl->sk_receive_queue.qlen)
  67. rtnl->sk_data_ready(rtnl, 0);
  68. netdev_run_todo();
  69. }
  70. int rtnl_trylock(void)
  71. {
  72. return mutex_trylock(&rtnl_mutex);
  73. }
  74. int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
  75. {
  76. memset(tb, 0, sizeof(struct rtattr*)*maxattr);
  77. while (RTA_OK(rta, len)) {
  78. unsigned flavor = rta->rta_type;
  79. if (flavor && flavor <= maxattr)
  80. tb[flavor-1] = rta;
  81. rta = RTA_NEXT(rta, len);
  82. }
  83. return 0;
  84. }
  85. struct sock *rtnl;
  86. struct rtnetlink_link * rtnetlink_links[NPROTO];
  87. static const int rtm_min[RTM_NR_FAMILIES] =
  88. {
  89. [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  90. [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
  91. [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  92. [RTM_FAM(RTM_NEWNEIGH)] = NLMSG_LENGTH(sizeof(struct ndmsg)),
  93. [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  94. [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  95. [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  96. [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
  97. [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
  98. [RTM_FAM(RTM_NEWPREFIX)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  99. [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  100. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  101. [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)),
  102. };
  103. static const int rta_max[RTM_NR_FAMILIES] =
  104. {
  105. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  106. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  107. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  108. [RTM_FAM(RTM_NEWNEIGH)] = NDA_MAX,
  109. [RTM_FAM(RTM_NEWRULE)] = RTA_MAX,
  110. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  111. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  112. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  113. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  114. [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX,
  115. };
  116. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  117. {
  118. struct rtattr *rta;
  119. int size = RTA_LENGTH(attrlen);
  120. rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
  121. rta->rta_type = attrtype;
  122. rta->rta_len = size;
  123. memcpy(RTA_DATA(rta), data, attrlen);
  124. memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
  125. }
  126. size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
  127. {
  128. size_t ret = RTA_PAYLOAD(rta);
  129. char *src = RTA_DATA(rta);
  130. if (ret > 0 && src[ret - 1] == '\0')
  131. ret--;
  132. if (size > 0) {
  133. size_t len = (ret >= size) ? size - 1 : ret;
  134. memset(dest, 0, size);
  135. memcpy(dest, src, len);
  136. }
  137. return ret;
  138. }
  139. int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  140. {
  141. int err = 0;
  142. NETLINK_CB(skb).dst_group = group;
  143. if (echo)
  144. atomic_inc(&skb->users);
  145. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  146. if (echo)
  147. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  148. return err;
  149. }
  150. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  151. {
  152. struct rtattr *mx = (struct rtattr*)skb->tail;
  153. int i;
  154. RTA_PUT(skb, RTA_METRICS, 0, NULL);
  155. for (i=0; i<RTAX_MAX; i++) {
  156. if (metrics[i])
  157. RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
  158. }
  159. mx->rta_len = skb->tail - (u8*)mx;
  160. if (mx->rta_len == RTA_LENGTH(0))
  161. skb_trim(skb, (u8*)mx - skb->data);
  162. return 0;
  163. rtattr_failure:
  164. skb_trim(skb, (u8*)mx - skb->data);
  165. return -1;
  166. }
  167. static void set_operstate(struct net_device *dev, unsigned char transition)
  168. {
  169. unsigned char operstate = dev->operstate;
  170. switch(transition) {
  171. case IF_OPER_UP:
  172. if ((operstate == IF_OPER_DORMANT ||
  173. operstate == IF_OPER_UNKNOWN) &&
  174. !netif_dormant(dev))
  175. operstate = IF_OPER_UP;
  176. break;
  177. case IF_OPER_DORMANT:
  178. if (operstate == IF_OPER_UP ||
  179. operstate == IF_OPER_UNKNOWN)
  180. operstate = IF_OPER_DORMANT;
  181. break;
  182. };
  183. if (dev->operstate != operstate) {
  184. write_lock_bh(&dev_base_lock);
  185. dev->operstate = operstate;
  186. write_unlock_bh(&dev_base_lock);
  187. netdev_state_change(dev);
  188. }
  189. }
  190. static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  191. int type, u32 pid, u32 seq, u32 change,
  192. unsigned int flags)
  193. {
  194. struct ifinfomsg *r;
  195. struct nlmsghdr *nlh;
  196. unsigned char *b = skb->tail;
  197. nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
  198. r = NLMSG_DATA(nlh);
  199. r->ifi_family = AF_UNSPEC;
  200. r->__ifi_pad = 0;
  201. r->ifi_type = dev->type;
  202. r->ifi_index = dev->ifindex;
  203. r->ifi_flags = dev_get_flags(dev);
  204. r->ifi_change = change;
  205. RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
  206. if (1) {
  207. u32 txqlen = dev->tx_queue_len;
  208. RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
  209. }
  210. if (1) {
  211. u32 weight = dev->weight;
  212. RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
  213. }
  214. if (1) {
  215. u8 operstate = netif_running(dev)?dev->operstate:IF_OPER_DOWN;
  216. u8 link_mode = dev->link_mode;
  217. RTA_PUT(skb, IFLA_OPERSTATE, sizeof(operstate), &operstate);
  218. RTA_PUT(skb, IFLA_LINKMODE, sizeof(link_mode), &link_mode);
  219. }
  220. if (1) {
  221. struct rtnl_link_ifmap map = {
  222. .mem_start = dev->mem_start,
  223. .mem_end = dev->mem_end,
  224. .base_addr = dev->base_addr,
  225. .irq = dev->irq,
  226. .dma = dev->dma,
  227. .port = dev->if_port,
  228. };
  229. RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  230. }
  231. if (dev->addr_len) {
  232. RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  233. RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  234. }
  235. if (1) {
  236. u32 mtu = dev->mtu;
  237. RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
  238. }
  239. if (dev->ifindex != dev->iflink) {
  240. u32 iflink = dev->iflink;
  241. RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
  242. }
  243. if (dev->qdisc_sleeping)
  244. RTA_PUT(skb, IFLA_QDISC,
  245. strlen(dev->qdisc_sleeping->ops->id) + 1,
  246. dev->qdisc_sleeping->ops->id);
  247. if (dev->master) {
  248. u32 master = dev->master->ifindex;
  249. RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
  250. }
  251. if (dev->get_stats) {
  252. unsigned long *stats = (unsigned long*)dev->get_stats(dev);
  253. if (stats) {
  254. struct rtattr *a;
  255. __u32 *s;
  256. int i;
  257. int n = sizeof(struct rtnl_link_stats)/4;
  258. a = __RTA_PUT(skb, IFLA_STATS, n*4);
  259. s = RTA_DATA(a);
  260. for (i=0; i<n; i++)
  261. s[i] = stats[i];
  262. }
  263. }
  264. nlh->nlmsg_len = skb->tail - b;
  265. return skb->len;
  266. nlmsg_failure:
  267. rtattr_failure:
  268. skb_trim(skb, b - skb->data);
  269. return -1;
  270. }
  271. static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  272. {
  273. int idx;
  274. int s_idx = cb->args[0];
  275. struct net_device *dev;
  276. read_lock(&dev_base_lock);
  277. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  278. if (idx < s_idx)
  279. continue;
  280. if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
  281. NETLINK_CB(cb->skb).pid,
  282. cb->nlh->nlmsg_seq, 0,
  283. NLM_F_MULTI) <= 0)
  284. break;
  285. }
  286. read_unlock(&dev_base_lock);
  287. cb->args[0] = idx;
  288. return skb->len;
  289. }
  290. static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  291. {
  292. struct ifinfomsg *ifm = NLMSG_DATA(nlh);
  293. struct rtattr **ida = arg;
  294. struct net_device *dev;
  295. int err, send_addr_notify = 0;
  296. if (ifm->ifi_index >= 0)
  297. dev = dev_get_by_index(ifm->ifi_index);
  298. else if (ida[IFLA_IFNAME - 1]) {
  299. char ifname[IFNAMSIZ];
  300. if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
  301. IFNAMSIZ) >= IFNAMSIZ)
  302. return -EINVAL;
  303. dev = dev_get_by_name(ifname);
  304. } else
  305. return -EINVAL;
  306. if (!dev)
  307. return -ENODEV;
  308. err = -EINVAL;
  309. if (ifm->ifi_flags)
  310. dev_change_flags(dev, ifm->ifi_flags);
  311. if (ida[IFLA_MAP - 1]) {
  312. struct rtnl_link_ifmap *u_map;
  313. struct ifmap k_map;
  314. if (!dev->set_config) {
  315. err = -EOPNOTSUPP;
  316. goto out;
  317. }
  318. if (!netif_device_present(dev)) {
  319. err = -ENODEV;
  320. goto out;
  321. }
  322. if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
  323. goto out;
  324. u_map = RTA_DATA(ida[IFLA_MAP - 1]);
  325. k_map.mem_start = (unsigned long) u_map->mem_start;
  326. k_map.mem_end = (unsigned long) u_map->mem_end;
  327. k_map.base_addr = (unsigned short) u_map->base_addr;
  328. k_map.irq = (unsigned char) u_map->irq;
  329. k_map.dma = (unsigned char) u_map->dma;
  330. k_map.port = (unsigned char) u_map->port;
  331. err = dev->set_config(dev, &k_map);
  332. if (err)
  333. goto out;
  334. }
  335. if (ida[IFLA_ADDRESS - 1]) {
  336. if (!dev->set_mac_address) {
  337. err = -EOPNOTSUPP;
  338. goto out;
  339. }
  340. if (!netif_device_present(dev)) {
  341. err = -ENODEV;
  342. goto out;
  343. }
  344. if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
  345. goto out;
  346. err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
  347. if (err)
  348. goto out;
  349. send_addr_notify = 1;
  350. }
  351. if (ida[IFLA_BROADCAST - 1]) {
  352. if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
  353. goto out;
  354. memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
  355. dev->addr_len);
  356. send_addr_notify = 1;
  357. }
  358. if (ida[IFLA_MTU - 1]) {
  359. if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  360. goto out;
  361. err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
  362. if (err)
  363. goto out;
  364. }
  365. if (ida[IFLA_TXQLEN - 1]) {
  366. if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  367. goto out;
  368. dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
  369. }
  370. if (ida[IFLA_WEIGHT - 1]) {
  371. if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  372. goto out;
  373. dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
  374. }
  375. if (ida[IFLA_OPERSTATE - 1]) {
  376. if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
  377. goto out;
  378. set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1])));
  379. }
  380. if (ida[IFLA_LINKMODE - 1]) {
  381. if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
  382. goto out;
  383. write_lock_bh(&dev_base_lock);
  384. dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1]));
  385. write_unlock_bh(&dev_base_lock);
  386. }
  387. if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
  388. char ifname[IFNAMSIZ];
  389. if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
  390. IFNAMSIZ) >= IFNAMSIZ)
  391. goto out;
  392. err = dev_change_name(dev, ifname);
  393. if (err)
  394. goto out;
  395. }
  396. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  397. if (ida[IFLA_WIRELESS - 1]) {
  398. /* Call Wireless Extensions.
  399. * Various stuff checked in there... */
  400. err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len);
  401. if (err)
  402. goto out;
  403. }
  404. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  405. err = 0;
  406. out:
  407. if (send_addr_notify)
  408. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  409. dev_put(dev);
  410. return err;
  411. }
  412. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  413. static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg)
  414. {
  415. struct ifinfomsg *ifm = NLMSG_DATA(in_nlh);
  416. struct rtattr **ida = arg;
  417. struct net_device *dev;
  418. struct ifinfomsg *r;
  419. struct nlmsghdr *nlh;
  420. int err = -ENOBUFS;
  421. struct sk_buff *skb;
  422. unsigned char *b;
  423. char *iw_buf = NULL;
  424. int iw_buf_len = 0;
  425. if (ifm->ifi_index >= 0)
  426. dev = dev_get_by_index(ifm->ifi_index);
  427. else
  428. return -EINVAL;
  429. if (!dev)
  430. return -ENODEV;
  431. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  432. if (ida[IFLA_WIRELESS - 1]) {
  433. /* Call Wireless Extensions. We need to know the size before
  434. * we can alloc. Various stuff checked in there... */
  435. err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len);
  436. if (err)
  437. goto out;
  438. }
  439. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  440. /* Create a skb big enough to include all the data.
  441. * Some requests are way bigger than 4k... Jean II */
  442. skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)),
  443. GFP_KERNEL);
  444. if (!skb)
  445. goto out;
  446. b = skb->tail;
  447. /* Put in the message the usual good stuff */
  448. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq,
  449. RTM_NEWLINK, sizeof(*r));
  450. r = NLMSG_DATA(nlh);
  451. r->ifi_family = AF_UNSPEC;
  452. r->__ifi_pad = 0;
  453. r->ifi_type = dev->type;
  454. r->ifi_index = dev->ifindex;
  455. r->ifi_flags = dev->flags;
  456. r->ifi_change = 0;
  457. /* Put the wireless payload if it exist */
  458. if(iw_buf != NULL)
  459. RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len,
  460. iw_buf + IW_EV_POINT_OFF);
  461. nlh->nlmsg_len = skb->tail - b;
  462. /* Needed ? */
  463. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  464. err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  465. if (err > 0)
  466. err = 0;
  467. out:
  468. if(iw_buf != NULL)
  469. kfree(iw_buf);
  470. dev_put(dev);
  471. return err;
  472. rtattr_failure:
  473. nlmsg_failure:
  474. kfree_skb(skb);
  475. goto out;
  476. }
  477. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  478. static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  479. {
  480. int idx;
  481. int s_idx = cb->family;
  482. if (s_idx == 0)
  483. s_idx = 1;
  484. for (idx=1; idx<NPROTO; idx++) {
  485. int type = cb->nlh->nlmsg_type-RTM_BASE;
  486. if (idx < s_idx || idx == PF_PACKET)
  487. continue;
  488. if (rtnetlink_links[idx] == NULL ||
  489. rtnetlink_links[idx][type].dumpit == NULL)
  490. continue;
  491. if (idx > s_idx)
  492. memset(&cb->args[0], 0, sizeof(cb->args));
  493. if (rtnetlink_links[idx][type].dumpit(skb, cb))
  494. break;
  495. }
  496. cb->family = idx;
  497. return skb->len;
  498. }
  499. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  500. {
  501. struct sk_buff *skb;
  502. int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
  503. sizeof(struct rtnl_link_ifmap) +
  504. sizeof(struct rtnl_link_stats) + 128);
  505. skb = alloc_skb(size, GFP_KERNEL);
  506. if (!skb)
  507. return;
  508. if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
  509. kfree_skb(skb);
  510. return;
  511. }
  512. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  513. netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
  514. }
  515. /* Protected by RTNL sempahore. */
  516. static struct rtattr **rta_buf;
  517. static int rtattr_max;
  518. /* Process one rtnetlink message. */
  519. static __inline__ int
  520. rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  521. {
  522. struct rtnetlink_link *link;
  523. struct rtnetlink_link *link_tab;
  524. int sz_idx, kind;
  525. int min_len;
  526. int family;
  527. int type;
  528. int err;
  529. /* Only requests are handled by kernel now */
  530. if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
  531. return 0;
  532. type = nlh->nlmsg_type;
  533. /* A control message: ignore them */
  534. if (type < RTM_BASE)
  535. return 0;
  536. /* Unknown message: reply with EINVAL */
  537. if (type > RTM_MAX)
  538. goto err_inval;
  539. type -= RTM_BASE;
  540. /* All the messages must have at least 1 byte length */
  541. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  542. return 0;
  543. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  544. if (family >= NPROTO) {
  545. *errp = -EAFNOSUPPORT;
  546. return -1;
  547. }
  548. link_tab = rtnetlink_links[family];
  549. if (link_tab == NULL)
  550. link_tab = rtnetlink_links[PF_UNSPEC];
  551. link = &link_tab[type];
  552. sz_idx = type>>2;
  553. kind = type&3;
  554. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
  555. *errp = -EPERM;
  556. return -1;
  557. }
  558. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  559. if (link->dumpit == NULL)
  560. link = &(rtnetlink_links[PF_UNSPEC][type]);
  561. if (link->dumpit == NULL)
  562. goto err_inval;
  563. if ((*errp = netlink_dump_start(rtnl, skb, nlh,
  564. link->dumpit, NULL)) != 0) {
  565. return -1;
  566. }
  567. netlink_queue_skip(nlh, skb);
  568. return -1;
  569. }
  570. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  571. min_len = rtm_min[sz_idx];
  572. if (nlh->nlmsg_len < min_len)
  573. goto err_inval;
  574. if (nlh->nlmsg_len > min_len) {
  575. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  576. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  577. while (RTA_OK(attr, attrlen)) {
  578. unsigned flavor = attr->rta_type;
  579. if (flavor) {
  580. if (flavor > rta_max[sz_idx])
  581. goto err_inval;
  582. rta_buf[flavor-1] = attr;
  583. }
  584. attr = RTA_NEXT(attr, attrlen);
  585. }
  586. }
  587. if (link->doit == NULL)
  588. link = &(rtnetlink_links[PF_UNSPEC][type]);
  589. if (link->doit == NULL)
  590. goto err_inval;
  591. err = link->doit(skb, nlh, (void *)&rta_buf[0]);
  592. *errp = err;
  593. return err;
  594. err_inval:
  595. *errp = -EINVAL;
  596. return -1;
  597. }
  598. static void rtnetlink_rcv(struct sock *sk, int len)
  599. {
  600. unsigned int qlen = 0;
  601. do {
  602. mutex_lock(&rtnl_mutex);
  603. netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
  604. mutex_unlock(&rtnl_mutex);
  605. netdev_run_todo();
  606. } while (qlen);
  607. }
  608. static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
  609. {
  610. [RTM_GETLINK - RTM_BASE] = {
  611. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  612. .doit = do_getlink,
  613. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  614. .dumpit = rtnetlink_dump_ifinfo },
  615. [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
  616. [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  617. [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  618. [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
  619. [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
  620. [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
  621. [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  622. [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
  623. [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
  624. };
  625. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  626. {
  627. struct net_device *dev = ptr;
  628. switch (event) {
  629. case NETDEV_UNREGISTER:
  630. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  631. break;
  632. case NETDEV_REGISTER:
  633. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  634. break;
  635. case NETDEV_UP:
  636. case NETDEV_DOWN:
  637. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  638. break;
  639. case NETDEV_CHANGE:
  640. case NETDEV_GOING_DOWN:
  641. break;
  642. default:
  643. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  644. break;
  645. }
  646. return NOTIFY_DONE;
  647. }
  648. static struct notifier_block rtnetlink_dev_notifier = {
  649. .notifier_call = rtnetlink_event,
  650. };
  651. void __init rtnetlink_init(void)
  652. {
  653. int i;
  654. rtattr_max = 0;
  655. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  656. if (rta_max[i] > rtattr_max)
  657. rtattr_max = rta_max[i];
  658. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  659. if (!rta_buf)
  660. panic("rtnetlink_init: cannot allocate rta_buf\n");
  661. rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
  662. THIS_MODULE);
  663. if (rtnl == NULL)
  664. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  665. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  666. register_netdevice_notifier(&rtnetlink_dev_notifier);
  667. rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
  668. rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
  669. }
  670. EXPORT_SYMBOL(__rta_fill);
  671. EXPORT_SYMBOL(rtattr_strlcpy);
  672. EXPORT_SYMBOL(rtattr_parse);
  673. EXPORT_SYMBOL(rtnetlink_links);
  674. EXPORT_SYMBOL(rtnetlink_put_metrics);
  675. EXPORT_SYMBOL(rtnl);
  676. EXPORT_SYMBOL(rtnl_lock);
  677. EXPORT_SYMBOL(rtnl_trylock);
  678. EXPORT_SYMBOL(rtnl_unlock);