rtnetlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. struct sockaddr *sa;
  337. int len;
  338. if (!dev->set_mac_address) {
  339. err = -EOPNOTSUPP;
  340. goto out;
  341. }
  342. if (!netif_device_present(dev)) {
  343. err = -ENODEV;
  344. goto out;
  345. }
  346. if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
  347. goto out;
  348. len = sizeof(sa_family_t) + dev->addr_len;
  349. sa = kmalloc(len, GFP_KERNEL);
  350. if (!sa) {
  351. err = -ENOMEM;
  352. goto out;
  353. }
  354. sa->sa_family = dev->type;
  355. memcpy(sa->sa_data, RTA_DATA(ida[IFLA_ADDRESS - 1]),
  356. dev->addr_len);
  357. err = dev->set_mac_address(dev, sa);
  358. kfree(sa);
  359. if (err)
  360. goto out;
  361. send_addr_notify = 1;
  362. }
  363. if (ida[IFLA_BROADCAST - 1]) {
  364. if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
  365. goto out;
  366. memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
  367. dev->addr_len);
  368. send_addr_notify = 1;
  369. }
  370. if (ida[IFLA_MTU - 1]) {
  371. if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  372. goto out;
  373. err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
  374. if (err)
  375. goto out;
  376. }
  377. if (ida[IFLA_TXQLEN - 1]) {
  378. if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  379. goto out;
  380. dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
  381. }
  382. if (ida[IFLA_WEIGHT - 1]) {
  383. if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
  384. goto out;
  385. dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
  386. }
  387. if (ida[IFLA_OPERSTATE - 1]) {
  388. if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
  389. goto out;
  390. set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1])));
  391. }
  392. if (ida[IFLA_LINKMODE - 1]) {
  393. if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
  394. goto out;
  395. write_lock_bh(&dev_base_lock);
  396. dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1]));
  397. write_unlock_bh(&dev_base_lock);
  398. }
  399. if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
  400. char ifname[IFNAMSIZ];
  401. if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
  402. IFNAMSIZ) >= IFNAMSIZ)
  403. goto out;
  404. err = dev_change_name(dev, ifname);
  405. if (err)
  406. goto out;
  407. }
  408. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  409. if (ida[IFLA_WIRELESS - 1]) {
  410. /* Call Wireless Extensions.
  411. * Various stuff checked in there... */
  412. err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len);
  413. if (err)
  414. goto out;
  415. }
  416. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  417. err = 0;
  418. out:
  419. if (send_addr_notify)
  420. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  421. dev_put(dev);
  422. return err;
  423. }
  424. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  425. static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg)
  426. {
  427. struct ifinfomsg *ifm = NLMSG_DATA(in_nlh);
  428. struct rtattr **ida = arg;
  429. struct net_device *dev;
  430. struct ifinfomsg *r;
  431. struct nlmsghdr *nlh;
  432. int err = -ENOBUFS;
  433. struct sk_buff *skb;
  434. unsigned char *b;
  435. char *iw_buf = NULL;
  436. int iw_buf_len = 0;
  437. if (ifm->ifi_index >= 0)
  438. dev = dev_get_by_index(ifm->ifi_index);
  439. else
  440. return -EINVAL;
  441. if (!dev)
  442. return -ENODEV;
  443. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  444. if (ida[IFLA_WIRELESS - 1]) {
  445. /* Call Wireless Extensions. We need to know the size before
  446. * we can alloc. Various stuff checked in there... */
  447. err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len);
  448. if (err)
  449. goto out;
  450. }
  451. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  452. /* Create a skb big enough to include all the data.
  453. * Some requests are way bigger than 4k... Jean II */
  454. skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)),
  455. GFP_KERNEL);
  456. if (!skb)
  457. goto out;
  458. b = skb->tail;
  459. /* Put in the message the usual good stuff */
  460. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq,
  461. RTM_NEWLINK, sizeof(*r));
  462. r = NLMSG_DATA(nlh);
  463. r->ifi_family = AF_UNSPEC;
  464. r->__ifi_pad = 0;
  465. r->ifi_type = dev->type;
  466. r->ifi_index = dev->ifindex;
  467. r->ifi_flags = dev->flags;
  468. r->ifi_change = 0;
  469. /* Put the wireless payload if it exist */
  470. if(iw_buf != NULL)
  471. RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len,
  472. iw_buf + IW_EV_POINT_OFF);
  473. nlh->nlmsg_len = skb->tail - b;
  474. /* Needed ? */
  475. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  476. err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  477. if (err > 0)
  478. err = 0;
  479. out:
  480. if(iw_buf != NULL)
  481. kfree(iw_buf);
  482. dev_put(dev);
  483. return err;
  484. rtattr_failure:
  485. nlmsg_failure:
  486. kfree_skb(skb);
  487. goto out;
  488. }
  489. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  490. static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  491. {
  492. int idx;
  493. int s_idx = cb->family;
  494. if (s_idx == 0)
  495. s_idx = 1;
  496. for (idx=1; idx<NPROTO; idx++) {
  497. int type = cb->nlh->nlmsg_type-RTM_BASE;
  498. if (idx < s_idx || idx == PF_PACKET)
  499. continue;
  500. if (rtnetlink_links[idx] == NULL ||
  501. rtnetlink_links[idx][type].dumpit == NULL)
  502. continue;
  503. if (idx > s_idx)
  504. memset(&cb->args[0], 0, sizeof(cb->args));
  505. if (rtnetlink_links[idx][type].dumpit(skb, cb))
  506. break;
  507. }
  508. cb->family = idx;
  509. return skb->len;
  510. }
  511. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  512. {
  513. struct sk_buff *skb;
  514. int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
  515. sizeof(struct rtnl_link_ifmap) +
  516. sizeof(struct rtnl_link_stats) + 128);
  517. skb = alloc_skb(size, GFP_KERNEL);
  518. if (!skb)
  519. return;
  520. if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
  521. kfree_skb(skb);
  522. return;
  523. }
  524. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  525. netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
  526. }
  527. /* Protected by RTNL sempahore. */
  528. static struct rtattr **rta_buf;
  529. static int rtattr_max;
  530. /* Process one rtnetlink message. */
  531. static __inline__ int
  532. rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  533. {
  534. struct rtnetlink_link *link;
  535. struct rtnetlink_link *link_tab;
  536. int sz_idx, kind;
  537. int min_len;
  538. int family;
  539. int type;
  540. int err;
  541. /* Only requests are handled by kernel now */
  542. if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
  543. return 0;
  544. type = nlh->nlmsg_type;
  545. /* A control message: ignore them */
  546. if (type < RTM_BASE)
  547. return 0;
  548. /* Unknown message: reply with EINVAL */
  549. if (type > RTM_MAX)
  550. goto err_inval;
  551. type -= RTM_BASE;
  552. /* All the messages must have at least 1 byte length */
  553. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  554. return 0;
  555. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  556. if (family >= NPROTO) {
  557. *errp = -EAFNOSUPPORT;
  558. return -1;
  559. }
  560. link_tab = rtnetlink_links[family];
  561. if (link_tab == NULL)
  562. link_tab = rtnetlink_links[PF_UNSPEC];
  563. link = &link_tab[type];
  564. sz_idx = type>>2;
  565. kind = type&3;
  566. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
  567. *errp = -EPERM;
  568. return -1;
  569. }
  570. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  571. if (link->dumpit == NULL)
  572. link = &(rtnetlink_links[PF_UNSPEC][type]);
  573. if (link->dumpit == NULL)
  574. goto err_inval;
  575. if ((*errp = netlink_dump_start(rtnl, skb, nlh,
  576. link->dumpit, NULL)) != 0) {
  577. return -1;
  578. }
  579. netlink_queue_skip(nlh, skb);
  580. return -1;
  581. }
  582. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  583. min_len = rtm_min[sz_idx];
  584. if (nlh->nlmsg_len < min_len)
  585. goto err_inval;
  586. if (nlh->nlmsg_len > min_len) {
  587. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  588. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  589. while (RTA_OK(attr, attrlen)) {
  590. unsigned flavor = attr->rta_type;
  591. if (flavor) {
  592. if (flavor > rta_max[sz_idx])
  593. goto err_inval;
  594. rta_buf[flavor-1] = attr;
  595. }
  596. attr = RTA_NEXT(attr, attrlen);
  597. }
  598. }
  599. if (link->doit == NULL)
  600. link = &(rtnetlink_links[PF_UNSPEC][type]);
  601. if (link->doit == NULL)
  602. goto err_inval;
  603. err = link->doit(skb, nlh, (void *)&rta_buf[0]);
  604. *errp = err;
  605. return err;
  606. err_inval:
  607. *errp = -EINVAL;
  608. return -1;
  609. }
  610. static void rtnetlink_rcv(struct sock *sk, int len)
  611. {
  612. unsigned int qlen = 0;
  613. do {
  614. mutex_lock(&rtnl_mutex);
  615. netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
  616. mutex_unlock(&rtnl_mutex);
  617. netdev_run_todo();
  618. } while (qlen);
  619. }
  620. static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
  621. {
  622. [RTM_GETLINK - RTM_BASE] = {
  623. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  624. .doit = do_getlink,
  625. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  626. .dumpit = rtnetlink_dump_ifinfo },
  627. [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
  628. [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  629. [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  630. [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
  631. [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
  632. [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
  633. [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
  634. [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
  635. [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
  636. };
  637. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  638. {
  639. struct net_device *dev = ptr;
  640. switch (event) {
  641. case NETDEV_UNREGISTER:
  642. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  643. break;
  644. case NETDEV_REGISTER:
  645. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  646. break;
  647. case NETDEV_UP:
  648. case NETDEV_DOWN:
  649. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  650. break;
  651. case NETDEV_CHANGE:
  652. case NETDEV_GOING_DOWN:
  653. break;
  654. default:
  655. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  656. break;
  657. }
  658. return NOTIFY_DONE;
  659. }
  660. static struct notifier_block rtnetlink_dev_notifier = {
  661. .notifier_call = rtnetlink_event,
  662. };
  663. void __init rtnetlink_init(void)
  664. {
  665. int i;
  666. rtattr_max = 0;
  667. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  668. if (rta_max[i] > rtattr_max)
  669. rtattr_max = rta_max[i];
  670. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  671. if (!rta_buf)
  672. panic("rtnetlink_init: cannot allocate rta_buf\n");
  673. rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
  674. THIS_MODULE);
  675. if (rtnl == NULL)
  676. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  677. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  678. register_netdevice_notifier(&rtnetlink_dev_notifier);
  679. rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
  680. rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
  681. }
  682. EXPORT_SYMBOL(__rta_fill);
  683. EXPORT_SYMBOL(rtattr_strlcpy);
  684. EXPORT_SYMBOL(rtattr_parse);
  685. EXPORT_SYMBOL(rtnetlink_links);
  686. EXPORT_SYMBOL(rtnetlink_put_metrics);
  687. EXPORT_SYMBOL(rtnl);
  688. EXPORT_SYMBOL(rtnl_lock);
  689. EXPORT_SYMBOL(rtnl_trylock);
  690. EXPORT_SYMBOL(rtnl_unlock);