rtnetlink.c 25 KB

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