rtnetlink.c 20 KB

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