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/timer.h>
  24. #include <linux/string.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/capability.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/security.h>
  35. #include <linux/mutex.h>
  36. #include <linux/if_addr.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/fib_rules.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. static struct sock *rtnl;
  57. void rtnl_lock(void)
  58. {
  59. mutex_lock(&rtnl_mutex);
  60. }
  61. void __rtnl_unlock(void)
  62. {
  63. mutex_unlock(&rtnl_mutex);
  64. }
  65. void rtnl_unlock(void)
  66. {
  67. mutex_unlock(&rtnl_mutex);
  68. if (rtnl && rtnl->sk_receive_queue.qlen)
  69. rtnl->sk_data_ready(rtnl, 0);
  70. netdev_run_todo();
  71. }
  72. int rtnl_trylock(void)
  73. {
  74. return mutex_trylock(&rtnl_mutex);
  75. }
  76. int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
  77. {
  78. memset(tb, 0, sizeof(struct rtattr*)*maxattr);
  79. while (RTA_OK(rta, len)) {
  80. unsigned flavor = rta->rta_type;
  81. if (flavor && flavor <= maxattr)
  82. tb[flavor-1] = rta;
  83. rta = RTA_NEXT(rta, len);
  84. }
  85. return 0;
  86. }
  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_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
  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_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  99. [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
  100. };
  101. static const int rta_max[RTM_NR_FAMILIES] =
  102. {
  103. [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
  104. [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
  105. [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
  106. [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
  107. [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
  108. [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
  109. [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
  110. [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
  111. };
  112. void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  113. {
  114. struct rtattr *rta;
  115. int size = RTA_LENGTH(attrlen);
  116. rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
  117. rta->rta_type = attrtype;
  118. rta->rta_len = size;
  119. memcpy(RTA_DATA(rta), data, attrlen);
  120. memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
  121. }
  122. size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
  123. {
  124. size_t ret = RTA_PAYLOAD(rta);
  125. char *src = RTA_DATA(rta);
  126. if (ret > 0 && src[ret - 1] == '\0')
  127. ret--;
  128. if (size > 0) {
  129. size_t len = (ret >= size) ? size - 1 : ret;
  130. memset(dest, 0, size);
  131. memcpy(dest, src, len);
  132. }
  133. return ret;
  134. }
  135. int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
  136. {
  137. int err = 0;
  138. NETLINK_CB(skb).dst_group = group;
  139. if (echo)
  140. atomic_inc(&skb->users);
  141. netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
  142. if (echo)
  143. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  144. return err;
  145. }
  146. int rtnl_unicast(struct sk_buff *skb, u32 pid)
  147. {
  148. return nlmsg_unicast(rtnl, skb, pid);
  149. }
  150. int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
  151. struct nlmsghdr *nlh, gfp_t flags)
  152. {
  153. int report = 0;
  154. if (nlh)
  155. report = nlmsg_report(nlh);
  156. return nlmsg_notify(rtnl, skb, pid, group, report, flags);
  157. }
  158. void rtnl_set_sk_err(u32 group, int error)
  159. {
  160. netlink_set_err(rtnl, 0, group, error);
  161. }
  162. int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
  163. {
  164. struct nlattr *mx;
  165. int i, valid = 0;
  166. mx = nla_nest_start(skb, RTA_METRICS);
  167. if (mx == NULL)
  168. return -ENOBUFS;
  169. for (i = 0; i < RTAX_MAX; i++) {
  170. if (metrics[i]) {
  171. valid++;
  172. NLA_PUT_U32(skb, i+1, metrics[i]);
  173. }
  174. }
  175. if (!valid) {
  176. nla_nest_cancel(skb, mx);
  177. return 0;
  178. }
  179. return nla_nest_end(skb, mx);
  180. nla_put_failure:
  181. return nla_nest_cancel(skb, mx);
  182. }
  183. int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
  184. u32 ts, u32 tsage, long expires, u32 error)
  185. {
  186. struct rta_cacheinfo ci = {
  187. .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
  188. .rta_used = dst->__use,
  189. .rta_clntref = atomic_read(&(dst->__refcnt)),
  190. .rta_error = error,
  191. .rta_id = id,
  192. .rta_ts = ts,
  193. .rta_tsage = tsage,
  194. };
  195. if (expires)
  196. ci.rta_expires = jiffies_to_clock_t(expires);
  197. return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  198. }
  199. EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
  200. static void set_operstate(struct net_device *dev, unsigned char transition)
  201. {
  202. unsigned char operstate = dev->operstate;
  203. switch(transition) {
  204. case IF_OPER_UP:
  205. if ((operstate == IF_OPER_DORMANT ||
  206. operstate == IF_OPER_UNKNOWN) &&
  207. !netif_dormant(dev))
  208. operstate = IF_OPER_UP;
  209. break;
  210. case IF_OPER_DORMANT:
  211. if (operstate == IF_OPER_UP ||
  212. operstate == IF_OPER_UNKNOWN)
  213. operstate = IF_OPER_DORMANT;
  214. break;
  215. };
  216. if (dev->operstate != operstate) {
  217. write_lock_bh(&dev_base_lock);
  218. dev->operstate = operstate;
  219. write_unlock_bh(&dev_base_lock);
  220. netdev_state_change(dev);
  221. }
  222. }
  223. static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
  224. struct net_device_stats *b)
  225. {
  226. a->rx_packets = b->rx_packets;
  227. a->tx_packets = b->tx_packets;
  228. a->rx_bytes = b->rx_bytes;
  229. a->tx_bytes = b->tx_bytes;
  230. a->rx_errors = b->rx_errors;
  231. a->tx_errors = b->tx_errors;
  232. a->rx_dropped = b->rx_dropped;
  233. a->tx_dropped = b->tx_dropped;
  234. a->multicast = b->multicast;
  235. a->collisions = b->collisions;
  236. a->rx_length_errors = b->rx_length_errors;
  237. a->rx_over_errors = b->rx_over_errors;
  238. a->rx_crc_errors = b->rx_crc_errors;
  239. a->rx_frame_errors = b->rx_frame_errors;
  240. a->rx_fifo_errors = b->rx_fifo_errors;
  241. a->rx_missed_errors = b->rx_missed_errors;
  242. a->tx_aborted_errors = b->tx_aborted_errors;
  243. a->tx_carrier_errors = b->tx_carrier_errors;
  244. a->tx_fifo_errors = b->tx_fifo_errors;
  245. a->tx_heartbeat_errors = b->tx_heartbeat_errors;
  246. a->tx_window_errors = b->tx_window_errors;
  247. a->rx_compressed = b->rx_compressed;
  248. a->tx_compressed = b->tx_compressed;
  249. };
  250. static inline size_t if_nlmsg_size(int iwbuflen)
  251. {
  252. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  253. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  254. + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
  255. + nla_total_size(sizeof(struct rtnl_link_ifmap))
  256. + nla_total_size(sizeof(struct rtnl_link_stats))
  257. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  258. + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
  259. + nla_total_size(4) /* IFLA_TXQLEN */
  260. + nla_total_size(4) /* IFLA_WEIGHT */
  261. + nla_total_size(4) /* IFLA_MTU */
  262. + nla_total_size(4) /* IFLA_LINK */
  263. + nla_total_size(4) /* IFLA_MASTER */
  264. + nla_total_size(1) /* IFLA_OPERSTATE */
  265. + nla_total_size(1) /* IFLA_LINKMODE */
  266. + nla_total_size(iwbuflen);
  267. }
  268. static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
  269. void *iwbuf, int iwbuflen, int type, u32 pid,
  270. u32 seq, u32 change, unsigned int flags)
  271. {
  272. struct ifinfomsg *ifm;
  273. struct nlmsghdr *nlh;
  274. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
  275. if (nlh == NULL)
  276. return -EMSGSIZE;
  277. ifm = nlmsg_data(nlh);
  278. ifm->ifi_family = AF_UNSPEC;
  279. ifm->__ifi_pad = 0;
  280. ifm->ifi_type = dev->type;
  281. ifm->ifi_index = dev->ifindex;
  282. ifm->ifi_flags = dev_get_flags(dev);
  283. ifm->ifi_change = change;
  284. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  285. NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
  286. NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
  287. NLA_PUT_U8(skb, IFLA_OPERSTATE,
  288. netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
  289. NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
  290. NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
  291. if (dev->ifindex != dev->iflink)
  292. NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
  293. if (dev->master)
  294. NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
  295. if (dev->qdisc_sleeping)
  296. NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
  297. if (1) {
  298. struct rtnl_link_ifmap map = {
  299. .mem_start = dev->mem_start,
  300. .mem_end = dev->mem_end,
  301. .base_addr = dev->base_addr,
  302. .irq = dev->irq,
  303. .dma = dev->dma,
  304. .port = dev->if_port,
  305. };
  306. NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
  307. }
  308. if (dev->addr_len) {
  309. NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
  310. NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
  311. }
  312. if (dev->get_stats) {
  313. struct net_device_stats *stats = dev->get_stats(dev);
  314. if (stats) {
  315. struct nlattr *attr;
  316. attr = nla_reserve(skb, IFLA_STATS,
  317. sizeof(struct rtnl_link_stats));
  318. if (attr == NULL)
  319. goto nla_put_failure;
  320. copy_rtnl_link_stats(nla_data(attr), stats);
  321. }
  322. }
  323. if (iwbuf)
  324. NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
  325. return nlmsg_end(skb, nlh);
  326. nla_put_failure:
  327. nlmsg_cancel(skb, nlh);
  328. return -EMSGSIZE;
  329. }
  330. static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  331. {
  332. int idx;
  333. int s_idx = cb->args[0];
  334. struct net_device *dev;
  335. read_lock(&dev_base_lock);
  336. for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
  337. if (idx < s_idx)
  338. continue;
  339. if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
  340. NETLINK_CB(cb->skb).pid,
  341. cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
  342. break;
  343. }
  344. read_unlock(&dev_base_lock);
  345. cb->args[0] = idx;
  346. return skb->len;
  347. }
  348. static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
  349. [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
  350. [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
  351. [IFLA_MTU] = { .type = NLA_U32 },
  352. [IFLA_TXQLEN] = { .type = NLA_U32 },
  353. [IFLA_WEIGHT] = { .type = NLA_U32 },
  354. [IFLA_OPERSTATE] = { .type = NLA_U8 },
  355. [IFLA_LINKMODE] = { .type = NLA_U8 },
  356. };
  357. static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  358. {
  359. struct ifinfomsg *ifm;
  360. struct net_device *dev;
  361. int err, send_addr_notify = 0, modified = 0;
  362. struct nlattr *tb[IFLA_MAX+1];
  363. char ifname[IFNAMSIZ];
  364. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  365. if (err < 0)
  366. goto errout;
  367. if (tb[IFLA_IFNAME])
  368. nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
  369. else
  370. ifname[0] = '\0';
  371. err = -EINVAL;
  372. ifm = nlmsg_data(nlh);
  373. if (ifm->ifi_index >= 0)
  374. dev = dev_get_by_index(ifm->ifi_index);
  375. else if (tb[IFLA_IFNAME])
  376. dev = dev_get_by_name(ifname);
  377. else
  378. goto errout;
  379. if (dev == NULL) {
  380. err = -ENODEV;
  381. goto errout;
  382. }
  383. if (tb[IFLA_ADDRESS] &&
  384. nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
  385. goto errout_dev;
  386. if (tb[IFLA_BROADCAST] &&
  387. nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
  388. goto errout_dev;
  389. if (tb[IFLA_MAP]) {
  390. struct rtnl_link_ifmap *u_map;
  391. struct ifmap k_map;
  392. if (!dev->set_config) {
  393. err = -EOPNOTSUPP;
  394. goto errout_dev;
  395. }
  396. if (!netif_device_present(dev)) {
  397. err = -ENODEV;
  398. goto errout_dev;
  399. }
  400. u_map = nla_data(tb[IFLA_MAP]);
  401. k_map.mem_start = (unsigned long) u_map->mem_start;
  402. k_map.mem_end = (unsigned long) u_map->mem_end;
  403. k_map.base_addr = (unsigned short) u_map->base_addr;
  404. k_map.irq = (unsigned char) u_map->irq;
  405. k_map.dma = (unsigned char) u_map->dma;
  406. k_map.port = (unsigned char) u_map->port;
  407. err = dev->set_config(dev, &k_map);
  408. if (err < 0)
  409. goto errout_dev;
  410. modified = 1;
  411. }
  412. if (tb[IFLA_ADDRESS]) {
  413. struct sockaddr *sa;
  414. int len;
  415. if (!dev->set_mac_address) {
  416. err = -EOPNOTSUPP;
  417. goto errout_dev;
  418. }
  419. if (!netif_device_present(dev)) {
  420. err = -ENODEV;
  421. goto errout_dev;
  422. }
  423. len = sizeof(sa_family_t) + dev->addr_len;
  424. sa = kmalloc(len, GFP_KERNEL);
  425. if (!sa) {
  426. err = -ENOMEM;
  427. goto errout_dev;
  428. }
  429. sa->sa_family = dev->type;
  430. memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
  431. dev->addr_len);
  432. err = dev->set_mac_address(dev, sa);
  433. kfree(sa);
  434. if (err)
  435. goto errout_dev;
  436. send_addr_notify = 1;
  437. modified = 1;
  438. }
  439. if (tb[IFLA_MTU]) {
  440. err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  441. if (err < 0)
  442. goto errout_dev;
  443. modified = 1;
  444. }
  445. /*
  446. * Interface selected by interface index but interface
  447. * name provided implies that a name change has been
  448. * requested.
  449. */
  450. if (ifm->ifi_index >= 0 && ifname[0]) {
  451. err = dev_change_name(dev, ifname);
  452. if (err < 0)
  453. goto errout_dev;
  454. modified = 1;
  455. }
  456. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  457. if (tb[IFLA_WIRELESS]) {
  458. /* Call Wireless Extensions.
  459. * Various stuff checked in there... */
  460. err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
  461. nla_len(tb[IFLA_WIRELESS]));
  462. if (err < 0)
  463. goto errout_dev;
  464. }
  465. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  466. if (tb[IFLA_BROADCAST]) {
  467. nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
  468. send_addr_notify = 1;
  469. }
  470. if (ifm->ifi_flags)
  471. dev_change_flags(dev, ifm->ifi_flags);
  472. if (tb[IFLA_TXQLEN])
  473. dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
  474. if (tb[IFLA_WEIGHT])
  475. dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
  476. if (tb[IFLA_OPERSTATE])
  477. set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
  478. if (tb[IFLA_LINKMODE]) {
  479. write_lock_bh(&dev_base_lock);
  480. dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
  481. write_unlock_bh(&dev_base_lock);
  482. }
  483. err = 0;
  484. errout_dev:
  485. if (err < 0 && modified && net_ratelimit())
  486. printk(KERN_WARNING "A link change request failed with "
  487. "some changes comitted already. Interface %s may "
  488. "have been left with an inconsistent configuration, "
  489. "please check.\n", dev->name);
  490. if (send_addr_notify)
  491. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  492. dev_put(dev);
  493. errout:
  494. return err;
  495. }
  496. static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  497. {
  498. struct ifinfomsg *ifm;
  499. struct nlattr *tb[IFLA_MAX+1];
  500. struct net_device *dev = NULL;
  501. struct sk_buff *nskb;
  502. char *iw_buf = NULL, *iw = NULL;
  503. int iw_buf_len = 0;
  504. int err;
  505. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
  506. if (err < 0)
  507. return err;
  508. ifm = nlmsg_data(nlh);
  509. if (ifm->ifi_index >= 0) {
  510. dev = dev_get_by_index(ifm->ifi_index);
  511. if (dev == NULL)
  512. return -ENODEV;
  513. } else
  514. return -EINVAL;
  515. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  516. if (tb[IFLA_WIRELESS]) {
  517. /* Call Wireless Extensions. We need to know the size before
  518. * we can alloc. Various stuff checked in there... */
  519. err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
  520. nla_len(tb[IFLA_WIRELESS]),
  521. &iw_buf, &iw_buf_len);
  522. if (err < 0)
  523. goto errout;
  524. /* Payload is at an offset in buffer */
  525. iw = iw_buf + 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);