rtnetlink.c 24 KB

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