rtnetlink.c 23 KB

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