rtnetlink.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 <linux/if_addr.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/fib_rules.h>
  51. #include <net/netlink.h>
  52. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  53. #include <linux/wireless.h>
  54. #include <net/iw_handler.h>
  55. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  56. static DEFINE_MUTEX(rtnl_mutex);
  57. static struct sock *rtnl;
  58. void rtnl_lock(void)
  59. {
  60. mutex_lock(&rtnl_mutex);
  61. }
  62. void __rtnl_unlock(void)
  63. {
  64. mutex_unlock(&rtnl_mutex);
  65. }
  66. void rtnl_unlock(void)
  67. {
  68. mutex_unlock(&rtnl_mutex);
  69. if (rtnl && rtnl->sk_receive_queue.qlen)
  70. rtnl->sk_data_ready(rtnl, 0);
  71. netdev_run_todo();
  72. }
  73. int rtnl_trylock(void)
  74. {
  75. return mutex_trylock(&rtnl_mutex);
  76. }
  77. int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
  78. {
  79. memset(tb, 0, sizeof(struct rtattr*)*maxattr);
  80. while (RTA_OK(rta, len)) {
  81. unsigned flavor = rta->rta_type;
  82. if (flavor && flavor <= maxattr)
  83. tb[flavor-1] = rta;
  84. rta = RTA_NEXT(rta, len);
  85. }
  86. return 0;
  87. }
  88. struct rtnetlink_link * rtnetlink_links[NPROTO];
  89. static const int rtm_min[RTM_NR_FAMILIES] =
  90. {
  91. [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  92. [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
  93. [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
  94. [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
  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_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  100. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  101. };
  102. static const int rta_max[RTM_NR_FAMILIES] =
  103. {
  104. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  105. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  106. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  107. [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
  108. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  109. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  110. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  111. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  112. };
  113. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  114. {
  115. struct rtattr *rta;
  116. int size = RTA_LENGTH(attrlen);
  117. rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
  118. rta->rta_type = attrtype;
  119. rta->rta_len = size;
  120. memcpy(RTA_DATA(rta), data, attrlen);
  121. memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
  122. }
  123. size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
  124. {
  125. size_t ret = RTA_PAYLOAD(rta);
  126. char *src = RTA_DATA(rta);
  127. if (ret > 0 && src[ret - 1] == '\0')
  128. ret--;
  129. if (size > 0) {
  130. size_t len = (ret >= size) ? size - 1 : ret;
  131. memset(dest, 0, size);
  132. memcpy(dest, src, len);
  133. }
  134. return ret;
  135. }
  136. int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  137. {
  138. int err = 0;
  139. NETLINK_CB(skb).dst_group = group;
  140. if (echo)
  141. atomic_inc(&skb->users);
  142. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  143. if (echo)
  144. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  145. return err;
  146. }
  147. int rtnl_unicast(struct sk_buff *skb, u32 pid)
  148. {
  149. return nlmsg_unicast(rtnl, skb, pid);
  150. }
  151. int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
  152. struct nlmsghdr *nlh, gfp_t flags)
  153. {
  154. int report = 0;
  155. if (nlh)
  156. report = nlmsg_report(nlh);
  157. return nlmsg_notify(rtnl, skb, pid, group, report, flags);
  158. }
  159. void rtnl_set_sk_err(u32 group, int error)
  160. {
  161. netlink_set_err(rtnl, 0, group, error);
  162. }
  163. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  164. {
  165. struct nlattr *mx;
  166. int i, valid = 0;
  167. mx = nla_nest_start(skb, RTA_METRICS);
  168. if (mx == NULL)
  169. return -ENOBUFS;
  170. for (i = 0; i < RTAX_MAX; i++) {
  171. if (metrics[i]) {
  172. valid++;
  173. NLA_PUT_U32(skb, i+1, metrics[i]);
  174. }
  175. }
  176. if (!valid) {
  177. nla_nest_cancel(skb, mx);
  178. return 0;
  179. }
  180. return nla_nest_end(skb, mx);
  181. nla_put_failure:
  182. return nla_nest_cancel(skb, mx);
  183. }
  184. int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
  185. u32 ts, u32 tsage, long expires, u32 error)
  186. {
  187. struct rta_cacheinfo ci = {
  188. .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
  189. .rta_used = dst->__use,
  190. .rta_clntref = atomic_read(&(dst->__refcnt)),
  191. .rta_error = error,
  192. .rta_id = id,
  193. .rta_ts = ts,
  194. .rta_tsage = tsage,
  195. };
  196. if (expires)
  197. ci.rta_expires = jiffies_to_clock_t(expires);
  198. return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  199. }
  200. EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
  201. static void set_operstate(struct net_device *dev, unsigned char transition)
  202. {
  203. unsigned char operstate = dev->operstate;
  204. switch(transition) {
  205. case IF_OPER_UP:
  206. if ((operstate == IF_OPER_DORMANT ||
  207. operstate == IF_OPER_UNKNOWN) &&
  208. !netif_dormant(dev))
  209. operstate = IF_OPER_UP;
  210. break;
  211. case IF_OPER_DORMANT:
  212. if (operstate == IF_OPER_UP ||
  213. operstate == IF_OPER_UNKNOWN)
  214. operstate = IF_OPER_DORMANT;
  215. break;
  216. };
  217. if (dev->operstate != operstate) {
  218. write_lock_bh(&dev_base_lock);
  219. dev->operstate = operstate;
  220. write_unlock_bh(&dev_base_lock);
  221. netdev_state_change(dev);
  222. }
  223. }
  224. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  225. struct net_device_stats *b)
  226. {
  227. a->rx_packets = b->rx_packets;
  228. a->tx_packets = b->tx_packets;
  229. a->rx_bytes = b->rx_bytes;
  230. a->tx_bytes = b->tx_bytes;
  231. a->rx_errors = b->rx_errors;
  232. a->tx_errors = b->tx_errors;
  233. a->rx_dropped = b->rx_dropped;
  234. a->tx_dropped = b->tx_dropped;
  235. a->multicast = b->multicast;
  236. a->collisions = b->collisions;
  237. a->rx_length_errors = b->rx_length_errors;
  238. a->rx_over_errors = b->rx_over_errors;
  239. a->rx_crc_errors = b->rx_crc_errors;
  240. a->rx_frame_errors = b->rx_frame_errors;
  241. a->rx_fifo_errors = b->rx_fifo_errors;
  242. a->rx_missed_errors = b->rx_missed_errors;
  243. a->tx_aborted_errors = b->tx_aborted_errors;
  244. a->tx_carrier_errors = b->tx_carrier_errors;
  245. a->tx_fifo_errors = b->tx_fifo_errors;
  246. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  247. a->tx_window_errors = b->tx_window_errors;
  248. a->rx_compressed = b->rx_compressed;
  249. a->tx_compressed = b->tx_compressed;
  250. };
  251. static inline size_t if_nlmsg_size(int iwbuflen)
  252. {
  253. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  254. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  255. + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
  256. + nla_total_size(sizeof(struct rtnl_link_ifmap))
  257. + nla_total_size(sizeof(struct rtnl_link_stats))
  258. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  259. + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
  260. + nla_total_size(4) /* IFLA_TXQLEN */
  261. + nla_total_size(4) /* IFLA_WEIGHT */
  262. + nla_total_size(4) /* IFLA_MTU */
  263. + nla_total_size(4) /* IFLA_LINK */
  264. + nla_total_size(4) /* IFLA_MASTER */
  265. + nla_total_size(1) /* IFLA_OPERSTATE */
  266. + nla_total_size(1) /* IFLA_LINKMODE */
  267. + nla_total_size(iwbuflen);
  268. }
  269. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  270. void *iwbuf, int iwbuflen, int type, u32 pid,
  271. u32 seq, u32 change, unsigned int flags)
  272. {
  273. struct ifinfomsg *ifm;
  274. struct nlmsghdr *nlh;
  275. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  276. if (nlh == NULL)
  277. return -EMSGSIZE;
  278. ifm = nlmsg_data(nlh);
  279. ifm->ifi_family = AF_UNSPEC;
  280. ifm->__ifi_pad = 0;
  281. ifm->ifi_type = dev->type;
  282. ifm->ifi_index = dev->ifindex;
  283. ifm->ifi_flags = dev_get_flags(dev);
  284. ifm->ifi_change = change;
  285. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  286. NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
  287. NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
  288. NLA_PUT_U8(skb, IFLA_OPERSTATE,
  289. netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
  290. NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
  291. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  292. if (dev->ifindex != dev->iflink)
  293. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  294. if (dev->master)
  295. NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
  296. if (dev->qdisc_sleeping)
  297. NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
  298. if (1) {
  299. struct rtnl_link_ifmap map = {
  300. .mem_start = dev->mem_start,
  301. .mem_end = dev->mem_end,
  302. .base_addr = dev->base_addr,
  303. .irq = dev->irq,
  304. .dma = dev->dma,
  305. .port = dev->if_port,
  306. };
  307. NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  308. }
  309. if (dev->addr_len) {
  310. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  311. NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  312. }
  313. if (dev->get_stats) {
  314. struct net_device_stats *stats = dev->get_stats(dev);
  315. if (stats) {
  316. struct nlattr *attr;
  317. attr = nla_reserve(skb, IFLA_STATS,
  318. sizeof(struct rtnl_link_stats));
  319. if (attr == NULL)
  320. goto nla_put_failure;
  321. copy_rtnl_link_stats(nla_data(attr), stats);
  322. }
  323. }
  324. if (iwbuf)
  325. NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
  326. return nlmsg_end(skb, nlh);
  327. nla_put_failure:
  328. nlmsg_cancel(skb, nlh);
  329. return -EMSGSIZE;
  330. }
  331. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  332. {
  333. int idx;
  334. int s_idx = cb->args[0];
  335. struct net_device *dev;
  336. read_lock(&dev_base_lock);
  337. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  338. if (idx < s_idx)
  339. continue;
  340. if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
  341. NETLINK_CB(cb->skb).pid,
  342. cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
  343. break;
  344. }
  345. read_unlock(&dev_base_lock);
  346. cb->args[0] = idx;
  347. return skb->len;
  348. }
  349. static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
  350. [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
  351. [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
  352. [IFLA_MTU] = { .type = NLA_U32 },
  353. [IFLA_TXQLEN] = { .type = NLA_U32 },
  354. [IFLA_WEIGHT] = { .type = NLA_U32 },
  355. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  356. [IFLA_LINKMODE] = { .type = NLA_U8 },
  357. };
  358. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  359. {
  360. struct ifinfomsg *ifm;
  361. struct net_device *dev;
  362. int err, send_addr_notify = 0, modified = 0;
  363. struct nlattr *tb[IFLA_MAX+1];
  364. char ifname[IFNAMSIZ];
  365. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  366. if (err < 0)
  367. goto errout;
  368. if (tb[IFLA_IFNAME])
  369. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  370. else
  371. ifname[0] = '\0';
  372. err = -EINVAL;
  373. ifm = nlmsg_data(nlh);
  374. if (ifm->ifi_index >= 0)
  375. dev = dev_get_by_index(ifm->ifi_index);
  376. else if (tb[IFLA_IFNAME])
  377. dev = dev_get_by_name(ifname);
  378. else
  379. goto errout;
  380. if (dev == NULL) {
  381. err = -ENODEV;
  382. goto errout;
  383. }
  384. if (tb[IFLA_ADDRESS] &&
  385. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  386. goto errout_dev;
  387. if (tb[IFLA_BROADCAST] &&
  388. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  389. goto errout_dev;
  390. if (tb[IFLA_MAP]) {
  391. struct rtnl_link_ifmap *u_map;
  392. struct ifmap k_map;
  393. if (!dev->set_config) {
  394. err = -EOPNOTSUPP;
  395. goto errout_dev;
  396. }
  397. if (!netif_device_present(dev)) {
  398. err = -ENODEV;
  399. goto errout_dev;
  400. }
  401. u_map = nla_data(tb[IFLA_MAP]);
  402. k_map.mem_start = (unsigned long) u_map->mem_start;
  403. k_map.mem_end = (unsigned long) u_map->mem_end;
  404. k_map.base_addr = (unsigned short) u_map->base_addr;
  405. k_map.irq = (unsigned char) u_map->irq;
  406. k_map.dma = (unsigned char) u_map->dma;
  407. k_map.port = (unsigned char) u_map->port;
  408. err = dev->set_config(dev, &k_map);
  409. if (err < 0)
  410. goto errout_dev;
  411. modified = 1;
  412. }
  413. if (tb[IFLA_ADDRESS]) {
  414. struct sockaddr *sa;
  415. int len;
  416. if (!dev->set_mac_address) {
  417. err = -EOPNOTSUPP;
  418. goto errout_dev;
  419. }
  420. if (!netif_device_present(dev)) {
  421. err = -ENODEV;
  422. goto errout_dev;
  423. }
  424. len = sizeof(sa_family_t) + dev->addr_len;
  425. sa = kmalloc(len, GFP_KERNEL);
  426. if (!sa) {
  427. err = -ENOMEM;
  428. goto errout_dev;
  429. }
  430. sa->sa_family = dev->type;
  431. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  432. dev->addr_len);
  433. err = dev->set_mac_address(dev, sa);
  434. kfree(sa);
  435. if (err)
  436. goto errout_dev;
  437. send_addr_notify = 1;
  438. modified = 1;
  439. }
  440. if (tb[IFLA_MTU]) {
  441. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  442. if (err < 0)
  443. goto errout_dev;
  444. modified = 1;
  445. }
  446. /*
  447. * Interface selected by interface index but interface
  448. * name provided implies that a name change has been
  449. * requested.
  450. */
  451. if (ifm->ifi_index >= 0 && ifname[0]) {
  452. err = dev_change_name(dev, ifname);
  453. if (err < 0)
  454. goto errout_dev;
  455. modified = 1;
  456. }
  457. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  458. if (tb[IFLA_WIRELESS]) {
  459. /* Call Wireless Extensions.
  460. * Various stuff checked in there... */
  461. err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
  462. nla_len(tb[IFLA_WIRELESS]));
  463. if (err < 0)
  464. goto errout_dev;
  465. }
  466. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  467. if (tb[IFLA_BROADCAST]) {
  468. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  469. send_addr_notify = 1;
  470. }
  471. if (ifm->ifi_flags)
  472. dev_change_flags(dev, ifm->ifi_flags);
  473. if (tb[IFLA_TXQLEN])
  474. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  475. if (tb[IFLA_WEIGHT])
  476. dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
  477. if (tb[IFLA_OPERSTATE])
  478. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  479. if (tb[IFLA_LINKMODE]) {
  480. write_lock_bh(&dev_base_lock);
  481. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  482. write_unlock_bh(&dev_base_lock);
  483. }
  484. err = 0;
  485. errout_dev:
  486. if (err < 0 && modified && net_ratelimit())
  487. printk(KERN_WARNING "A link change request failed with "
  488. "some changes comitted already. Interface %s may "
  489. "have been left with an inconsistent configuration, "
  490. "please check.\n", dev->name);
  491. if (send_addr_notify)
  492. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  493. dev_put(dev);
  494. errout:
  495. return err;
  496. }
  497. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  498. {
  499. struct ifinfomsg *ifm;
  500. struct nlattr *tb[IFLA_MAX+1];
  501. struct net_device *dev = NULL;
  502. struct sk_buff *nskb;
  503. char *iw_buf = NULL, *iw = NULL;
  504. int iw_buf_len = 0;
  505. int err;
  506. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  507. if (err < 0)
  508. return err;
  509. ifm = nlmsg_data(nlh);
  510. if (ifm->ifi_index >= 0) {
  511. dev = dev_get_by_index(ifm->ifi_index);
  512. if (dev == NULL)
  513. return -ENODEV;
  514. } else
  515. return -EINVAL;
  516. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  517. if (tb[IFLA_WIRELESS]) {
  518. /* Call Wireless Extensions. We need to know the size before
  519. * we can alloc. Various stuff checked in there... */
  520. err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
  521. nla_len(tb[IFLA_WIRELESS]),
  522. &iw_buf, &iw_buf_len);
  523. if (err < 0)
  524. goto errout;
  525. iw += IW_EV_POINT_OFF;
  526. }
  527. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  528. nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL);
  529. if (nskb == NULL) {
  530. err = -ENOBUFS;
  531. goto errout;
  532. }
  533. err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
  534. NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
  535. if (err < 0) {
  536. /* -EMSGSIZE implies BUG in if_nlmsg_size */
  537. WARN_ON(err == -EMSGSIZE);
  538. kfree_skb(nskb);
  539. goto errout;
  540. }
  541. err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
  542. errout:
  543. kfree(iw_buf);
  544. dev_put(dev);
  545. return err;
  546. }
  547. static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
  548. {
  549. int idx;
  550. int s_idx = cb->family;
  551. if (s_idx == 0)
  552. s_idx = 1;
  553. for (idx=1; idx<NPROTO; idx++) {
  554. int type = cb->nlh->nlmsg_type-RTM_BASE;
  555. if (idx < s_idx || idx == PF_PACKET)
  556. continue;
  557. if (rtnetlink_links[idx] == NULL ||
  558. rtnetlink_links[idx][type].dumpit == NULL)
  559. continue;
  560. if (idx > s_idx)
  561. memset(&cb->args[0], 0, sizeof(cb->args));
  562. if (rtnetlink_links[idx][type].dumpit(skb, cb))
  563. break;
  564. }
  565. cb->family = idx;
  566. return skb->len;
  567. }
  568. void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
  569. {
  570. struct sk_buff *skb;
  571. int err = -ENOBUFS;
  572. skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL);
  573. if (skb == NULL)
  574. goto errout;
  575. err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0);
  576. if (err < 0) {
  577. /* -EMSGSIZE implies BUG in if_nlmsg_size() */
  578. WARN_ON(err == -EMSGSIZE);
  579. kfree_skb(skb);
  580. goto errout;
  581. }
  582. err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
  583. errout:
  584. if (err < 0)
  585. rtnl_set_sk_err(RTNLGRP_LINK, err);
  586. }
  587. /* Protected by RTNL sempahore. */
  588. static struct rtattr **rta_buf;
  589. static int rtattr_max;
  590. /* Process one rtnetlink message. */
  591. static __inline__ int
  592. rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  593. {
  594. struct rtnetlink_link *link;
  595. struct rtnetlink_link *link_tab;
  596. int sz_idx, kind;
  597. int min_len;
  598. int family;
  599. int type;
  600. int err;
  601. /* Only requests are handled by kernel now */
  602. if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
  603. return 0;
  604. type = nlh->nlmsg_type;
  605. /* A control message: ignore them */
  606. if (type < RTM_BASE)
  607. return 0;
  608. /* Unknown message: reply with EINVAL */
  609. if (type > RTM_MAX)
  610. goto err_inval;
  611. type -= RTM_BASE;
  612. /* All the messages must have at least 1 byte length */
  613. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  614. return 0;
  615. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  616. if (family >= NPROTO) {
  617. *errp = -EAFNOSUPPORT;
  618. return -1;
  619. }
  620. link_tab = rtnetlink_links[family];
  621. if (link_tab == NULL)
  622. link_tab = rtnetlink_links[PF_UNSPEC];
  623. link = &link_tab[type];
  624. sz_idx = type>>2;
  625. kind = type&3;
  626. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
  627. *errp = -EPERM;
  628. return -1;
  629. }
  630. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  631. if (link->dumpit == NULL)
  632. link = &(rtnetlink_links[PF_UNSPEC][type]);
  633. if (link->dumpit == NULL)
  634. goto err_inval;
  635. if ((*errp = netlink_dump_start(rtnl, skb, nlh,
  636. link->dumpit, NULL)) != 0) {
  637. return -1;
  638. }
  639. netlink_queue_skip(nlh, skb);
  640. return -1;
  641. }
  642. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  643. min_len = rtm_min[sz_idx];
  644. if (nlh->nlmsg_len < min_len)
  645. goto err_inval;
  646. if (nlh->nlmsg_len > min_len) {
  647. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  648. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  649. while (RTA_OK(attr, attrlen)) {
  650. unsigned flavor = attr->rta_type;
  651. if (flavor) {
  652. if (flavor > rta_max[sz_idx])
  653. goto err_inval;
  654. rta_buf[flavor-1] = attr;
  655. }
  656. attr = RTA_NEXT(attr, attrlen);
  657. }
  658. }
  659. if (link->doit == NULL)
  660. link = &(rtnetlink_links[PF_UNSPEC][type]);
  661. if (link->doit == NULL)
  662. goto err_inval;
  663. err = link->doit(skb, nlh, (void *)&rta_buf[0]);
  664. *errp = err;
  665. return err;
  666. err_inval:
  667. *errp = -EINVAL;
  668. return -1;
  669. }
  670. static void rtnetlink_rcv(struct sock *sk, int len)
  671. {
  672. unsigned int qlen = 0;
  673. do {
  674. mutex_lock(&rtnl_mutex);
  675. netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
  676. mutex_unlock(&rtnl_mutex);
  677. netdev_run_todo();
  678. } while (qlen);
  679. }
  680. static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
  681. {
  682. [RTM_GETLINK - RTM_BASE] = { .doit = rtnl_getlink,
  683. .dumpit = rtnl_dump_ifinfo },
  684. [RTM_SETLINK - RTM_BASE] = { .doit = rtnl_setlink },
  685. [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnl_dump_all },
  686. [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnl_dump_all },
  687. [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
  688. [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
  689. [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
  690. #ifdef CONFIG_FIB_RULES
  691. [RTM_NEWRULE - RTM_BASE] = { .doit = fib_nl_newrule },
  692. [RTM_DELRULE - RTM_BASE] = { .doit = fib_nl_delrule },
  693. #endif
  694. [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnl_dump_all },
  695. [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
  696. [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
  697. };
  698. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  699. {
  700. struct net_device *dev = ptr;
  701. switch (event) {
  702. case NETDEV_UNREGISTER:
  703. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  704. break;
  705. case NETDEV_REGISTER:
  706. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  707. break;
  708. case NETDEV_UP:
  709. case NETDEV_DOWN:
  710. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  711. break;
  712. case NETDEV_CHANGE:
  713. case NETDEV_GOING_DOWN:
  714. break;
  715. default:
  716. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  717. break;
  718. }
  719. return NOTIFY_DONE;
  720. }
  721. static struct notifier_block rtnetlink_dev_notifier = {
  722. .notifier_call = rtnetlink_event,
  723. };
  724. void __init rtnetlink_init(void)
  725. {
  726. int i;
  727. rtattr_max = 0;
  728. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  729. if (rta_max[i] > rtattr_max)
  730. rtattr_max = rta_max[i];
  731. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  732. if (!rta_buf)
  733. panic("rtnetlink_init: cannot allocate rta_buf\n");
  734. rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
  735. THIS_MODULE);
  736. if (rtnl == NULL)
  737. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  738. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  739. register_netdevice_notifier(&rtnetlink_dev_notifier);
  740. rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
  741. rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
  742. }
  743. EXPORT_SYMBOL(__rta_fill);
  744. EXPORT_SYMBOL(rtattr_strlcpy);
  745. EXPORT_SYMBOL(rtattr_parse);
  746. EXPORT_SYMBOL(rtnetlink_links);
  747. EXPORT_SYMBOL(rtnetlink_put_metrics);
  748. EXPORT_SYMBOL(rtnl_lock);
  749. EXPORT_SYMBOL(rtnl_trylock);
  750. EXPORT_SYMBOL(rtnl_unlock);
  751. EXPORT_SYMBOL(rtnl_unicast);
  752. EXPORT_SYMBOL(rtnl_notify);
  753. EXPORT_SYMBOL(rtnl_set_sk_err);