rtnetlink.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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 int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  714. {
  715. rtnl_doit_func doit;
  716. int sz_idx, kind;
  717. int min_len;
  718. int family;
  719. int type;
  720. type = nlh->nlmsg_type;
  721. if (type > RTM_MAX)
  722. return -EOPNOTSUPP;
  723. type -= RTM_BASE;
  724. /* All the messages must have at least 1 byte length */
  725. if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
  726. return 0;
  727. family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
  728. if (family >= NPROTO)
  729. return -EAFNOSUPPORT;
  730. sz_idx = type>>2;
  731. kind = type&3;
  732. if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
  733. return -EPERM;
  734. if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
  735. rtnl_dumpit_func dumpit;
  736. dumpit = rtnl_get_dumpit(family, type);
  737. if (dumpit == NULL)
  738. return -EOPNOTSUPP;
  739. return netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
  740. }
  741. memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
  742. min_len = rtm_min[sz_idx];
  743. if (nlh->nlmsg_len < min_len)
  744. return -EINVAL;
  745. if (nlh->nlmsg_len > min_len) {
  746. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  747. struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
  748. while (RTA_OK(attr, attrlen)) {
  749. unsigned flavor = attr->rta_type;
  750. if (flavor) {
  751. if (flavor > rta_max[sz_idx])
  752. return -EINVAL;
  753. rta_buf[flavor-1] = attr;
  754. }
  755. attr = RTA_NEXT(attr, attrlen);
  756. }
  757. }
  758. doit = rtnl_get_doit(family, type);
  759. if (doit == NULL)
  760. return -EOPNOTSUPP;
  761. return doit(skb, nlh, (void *)&rta_buf[0]);
  762. }
  763. static void rtnetlink_rcv(struct sock *sk, int len)
  764. {
  765. unsigned int qlen = 0;
  766. do {
  767. mutex_lock(&rtnl_mutex);
  768. netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
  769. mutex_unlock(&rtnl_mutex);
  770. netdev_run_todo();
  771. } while (qlen);
  772. }
  773. static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
  774. {
  775. struct net_device *dev = ptr;
  776. switch (event) {
  777. case NETDEV_UNREGISTER:
  778. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  779. break;
  780. case NETDEV_REGISTER:
  781. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  782. break;
  783. case NETDEV_UP:
  784. case NETDEV_DOWN:
  785. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  786. break;
  787. case NETDEV_CHANGE:
  788. case NETDEV_GOING_DOWN:
  789. break;
  790. default:
  791. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  792. break;
  793. }
  794. return NOTIFY_DONE;
  795. }
  796. static struct notifier_block rtnetlink_dev_notifier = {
  797. .notifier_call = rtnetlink_event,
  798. };
  799. void __init rtnetlink_init(void)
  800. {
  801. int i;
  802. rtattr_max = 0;
  803. for (i = 0; i < ARRAY_SIZE(rta_max); i++)
  804. if (rta_max[i] > rtattr_max)
  805. rtattr_max = rta_max[i];
  806. rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
  807. if (!rta_buf)
  808. panic("rtnetlink_init: cannot allocate rta_buf\n");
  809. rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
  810. NULL, THIS_MODULE);
  811. if (rtnl == NULL)
  812. panic("rtnetlink_init: cannot initialize rtnetlink\n");
  813. netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
  814. register_netdevice_notifier(&rtnetlink_dev_notifier);
  815. rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
  816. rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
  817. rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
  818. rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
  819. }
  820. EXPORT_SYMBOL(__rta_fill);
  821. EXPORT_SYMBOL(rtattr_strlcpy);
  822. EXPORT_SYMBOL(rtattr_parse);
  823. EXPORT_SYMBOL(rtnetlink_put_metrics);
  824. EXPORT_SYMBOL(rtnl_lock);
  825. EXPORT_SYMBOL(rtnl_trylock);
  826. EXPORT_SYMBOL(rtnl_unlock);
  827. EXPORT_SYMBOL(rtnl_unicast);
  828. EXPORT_SYMBOL(rtnl_notify);
  829. EXPORT_SYMBOL(rtnl_set_sk_err);