link.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * lib/route/link.c Links (Interfaces)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup rtnl
  13. * @defgroup link Links (Interfaces)
  14. * @brief
  15. *
  16. * @par Link Identification
  17. * A link can be identified by either its interface index or by its
  18. * name. The kernel favours the interface index but falls back to the
  19. * interface name if the interface index is lesser-than 0 for kernels
  20. * >= 2.6.11. Therefore you can request changes without mapping a
  21. * interface name to the corresponding index first.
  22. *
  23. * @par Changeable Attributes
  24. * @anchor link_changeable
  25. * - Link layer address
  26. * - Link layer broadcast address
  27. * - device mapping (ifmap) (>= 2.6.9)
  28. * - MTU (>= 2.6.9)
  29. * - Transmission queue length (>= 2.6.9)
  30. * - Weight (>= 2.6.9)
  31. * - Link name (only via access through interface index) (>= 2.6.9)
  32. * - Flags (>= 2.6.9)
  33. * - IFF_DEBUG
  34. * - IFF_NOTRAILERS
  35. * - IFF_NOARP
  36. * - IFF_DYNAMIC
  37. * - IFF_MULTICAST
  38. * - IFF_PORTSEL
  39. * - IFF_AUTOMEDIA
  40. * - IFF_UP
  41. * - IFF_PROMISC
  42. * - IFF_ALLMULTI
  43. *
  44. * @par Link Flags (linux/if.h)
  45. * @anchor link_flags
  46. * @code
  47. * IFF_UP Status of link (up|down)
  48. * IFF_BROADCAST Indicates this link allows broadcasting
  49. * IFF_MULTICAST Indicates this link allows multicasting
  50. * IFF_ALLMULTI Indicates this link is doing multicast routing
  51. * IFF_DEBUG Tell the driver to do debugging (currently unused)
  52. * IFF_LOOPBACK This is the loopback link
  53. * IFF_POINTOPOINT Point-to-point link
  54. * IFF_NOARP Link is unable to perform ARP
  55. * IFF_PROMISC Status of promiscious mode flag
  56. * IFF_MASTER Used by teql
  57. * IFF_SLAVE Used by teql
  58. * IFF_PORTSEL Indicates this link allows port selection
  59. * IFF_AUTOMEDIA Indicates this link selects port automatically
  60. * IFF_DYNAMIC Indicates the address of this link is dynamic
  61. * IFF_RUNNING Link is running and carrier is ok.
  62. * IFF_NOTRAILERS Unused, BSD compat.
  63. * @endcode
  64. *
  65. * @par Notes on IFF_PROMISC and IFF_ALLMULTI flags
  66. * Although you can query the status of IFF_PROMISC and IFF_ALLMULTI
  67. * they do not represent the actual state in the kernel but rather
  68. * whether the flag has been enabled/disabled by userspace. The link
  69. * may be in promiscious mode even if IFF_PROMISC is not set in a link
  70. * dump request response because promiscity might be needed by the driver
  71. * for a period of time.
  72. *
  73. * @note The unit of the transmission queue length depends on the
  74. * link type, a common unit is \a packets.
  75. *
  76. * @par 1) Retrieving information about available links
  77. * @code
  78. * // The first step is to retrieve a list of all available interfaces within
  79. * // the kernel and put them into a cache.
  80. * struct nl_cache *cache = rtnl_link_alloc_cache(nl_handle);
  81. *
  82. * // In a second step, a specific link may be looked up by either interface
  83. * // index or interface name.
  84. * struct rtnl_link *link = rtnl_link_get_by_name(cache, "lo");
  85. *
  86. * // rtnl_link_get_by_name() is the short version for translating the
  87. * // interface name to an interface index first like this:
  88. * int ifindex = rtnl_link_name2i(cache, "lo");
  89. * struct rtnl_link *link = rtnl_link_get(cache, ifindex);
  90. *
  91. * // After successful usage, the object must be given back to the cache
  92. * rtnl_link_put(link);
  93. * @endcode
  94. *
  95. * @par 2) Changing link attributes
  96. * @code
  97. * // In order to change any attributes of an existing link, we must allocate
  98. * // a new link to hold the change requests:
  99. * struct rtnl_link *request = rtnl_link_alloc();
  100. *
  101. * // Now we can go on and specify the attributes we want to change:
  102. * rtnl_link_set_weight(request, 300);
  103. * rtnl_link_set_mtu(request, 1360);
  104. *
  105. * // We can also shut an interface down administratively
  106. * rtnl_link_unset_flags(request, rtnl_link_str2flags("up"));
  107. *
  108. * // Actually, we should know which link to change, so let's look it up
  109. * struct rtnl_link *old = rtnl_link_get(cache, "eth0");
  110. *
  111. * // Two ways exist to commit this change request, the first one is to
  112. * // build the required netlink message and send it out in one single
  113. * // step:
  114. * rtnl_link_change(nl_handle, old, request);
  115. *
  116. * // An alternative way is to build the netlink message and send it
  117. * // out yourself using nl_send_auto_complete()
  118. * struct nl_msg *msg = rtnl_link_build_change_request(old, request);
  119. * nl_send_auto_complete(nl_handle, nlmsg_hdr(msg));
  120. * nlmsg_free(msg);
  121. *
  122. * // Don't forget to give back the link object ;->
  123. * rtnl_link_put(old);
  124. * @endcode
  125. *
  126. * @par 3) Link Type Specific Attributes
  127. * @code
  128. * // Some link types offer additional parameters and statistics specific
  129. * // to their type. F.e. a VLAN link can be configured like this:
  130. * //
  131. * // Allocate a new link and set the info type to "vlan". This is required
  132. * // to prepare the link to hold vlan specific attributes.
  133. * struct rtnl_link *request = rtnl_link_alloc();
  134. * rtnl_link_set_info_type(request, "vlan");
  135. *
  136. * // Now vlan specific attributes can be set:
  137. * rtnl_link_vlan_set_id(request, 10);
  138. * rtnl_link_vlan_set_ingress_map(request, 2, 8);
  139. *
  140. * // Of course the attributes can also be read, check the info type
  141. * // to make sure you are using the right access functions:
  142. * char *type = rtnl_link_get_info_type(link);
  143. * if (!strcmp(type, "vlan"))
  144. * int id = rtnl_link_vlan_get_id(link);
  145. * @endcode
  146. * @{
  147. */
  148. #include <netlink-local.h>
  149. #include <netlink/netlink.h>
  150. #include <netlink/attr.h>
  151. #include <netlink/utils.h>
  152. #include <netlink/object.h>
  153. #include <netlink/route/rtnl.h>
  154. #include <netlink/route/link.h>
  155. #include <netlink/route/link/info-api.h>
  156. /** @cond SKIP */
  157. #define LINK_ATTR_MTU 0x0001
  158. #define LINK_ATTR_LINK 0x0002
  159. #define LINK_ATTR_TXQLEN 0x0004
  160. #define LINK_ATTR_WEIGHT 0x0008
  161. #define LINK_ATTR_MASTER 0x0010
  162. #define LINK_ATTR_QDISC 0x0020
  163. #define LINK_ATTR_MAP 0x0040
  164. #define LINK_ATTR_ADDR 0x0080
  165. #define LINK_ATTR_BRD 0x0100
  166. #define LINK_ATTR_FLAGS 0x0200
  167. #define LINK_ATTR_IFNAME 0x0400
  168. #define LINK_ATTR_IFINDEX 0x0800
  169. #define LINK_ATTR_FAMILY 0x1000
  170. #define LINK_ATTR_ARPTYPE 0x2000
  171. #define LINK_ATTR_STATS 0x4000
  172. #define LINK_ATTR_CHANGE 0x8000
  173. #define LINK_ATTR_OPERSTATE 0x10000
  174. #define LINK_ATTR_LINKMODE 0x20000
  175. #define LINK_ATTR_LINKINFO 0x40000
  176. static struct nl_cache_ops rtnl_link_ops;
  177. static struct nl_object_ops link_obj_ops;
  178. /** @endcond */
  179. static void release_link_info(struct rtnl_link *link)
  180. {
  181. struct rtnl_link_info_ops *io = link->l_info_ops;
  182. if (io != NULL) {
  183. io->io_refcnt--;
  184. io->io_free(link);
  185. link->l_info_ops = NULL;
  186. }
  187. }
  188. static void link_free_data(struct nl_object *c)
  189. {
  190. struct rtnl_link *link = nl_object_priv(c);
  191. if (link) {
  192. struct rtnl_link_info_ops *io;
  193. if ((io = link->l_info_ops) != NULL)
  194. release_link_info(link);
  195. nl_addr_put(link->l_addr);
  196. nl_addr_put(link->l_bcast);
  197. }
  198. }
  199. static int link_clone(struct nl_object *_dst, struct nl_object *_src)
  200. {
  201. struct rtnl_link *dst = nl_object_priv(_dst);
  202. struct rtnl_link *src = nl_object_priv(_src);
  203. int err;
  204. if (src->l_addr)
  205. if (!(dst->l_addr = nl_addr_clone(src->l_addr)))
  206. goto errout;
  207. if (src->l_bcast)
  208. if (!(dst->l_bcast = nl_addr_clone(src->l_bcast)))
  209. goto errout;
  210. if (src->l_info_ops && src->l_info_ops->io_clone) {
  211. err = src->l_info_ops->io_clone(dst, src);
  212. if (err < 0)
  213. goto errout;
  214. }
  215. return 0;
  216. errout:
  217. return nl_get_errno();
  218. }
  219. static struct nla_policy link_policy[IFLA_MAX+1] = {
  220. [IFLA_IFNAME] = { .type = NLA_STRING,
  221. .maxlen = IFNAMSIZ },
  222. [IFLA_MTU] = { .type = NLA_U32 },
  223. [IFLA_TXQLEN] = { .type = NLA_U32 },
  224. [IFLA_LINK] = { .type = NLA_U32 },
  225. [IFLA_WEIGHT] = { .type = NLA_U32 },
  226. [IFLA_MASTER] = { .type = NLA_U32 },
  227. [IFLA_OPERSTATE]= { .type = NLA_U8 },
  228. [IFLA_LINKMODE] = { .type = NLA_U8 },
  229. [IFLA_LINKINFO] = { .type = NLA_NESTED },
  230. [IFLA_QDISC] = { .type = NLA_STRING,
  231. .maxlen = IFQDISCSIZ },
  232. [IFLA_STATS] = { .minlen = sizeof(struct rtnl_link_stats) },
  233. [IFLA_MAP] = { .minlen = sizeof(struct rtnl_link_ifmap) },
  234. };
  235. static struct nla_policy link_info_policy[IFLA_INFO_MAX+1] = {
  236. [IFLA_INFO_KIND] = { .type = NLA_STRING },
  237. [IFLA_INFO_DATA] = { .type = NLA_NESTED },
  238. [IFLA_INFO_XSTATS] = { .type = NLA_NESTED },
  239. };
  240. static int link_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  241. struct nlmsghdr *n, struct nl_parser_param *pp)
  242. {
  243. struct rtnl_link *link;
  244. struct ifinfomsg *ifi;
  245. struct nlattr *tb[IFLA_MAX+1];
  246. int err;
  247. link = rtnl_link_alloc();
  248. if (link == NULL) {
  249. err = nl_errno(ENOMEM);
  250. goto errout;
  251. }
  252. link->ce_msgtype = n->nlmsg_type;
  253. err = nlmsg_parse(n, sizeof(*ifi), tb, IFLA_MAX, link_policy);
  254. if (err < 0)
  255. goto errout;
  256. if (tb[IFLA_IFNAME] == NULL) {
  257. err = nl_error(EINVAL, "Missing link name TLV");
  258. goto errout;
  259. }
  260. nla_strlcpy(link->l_name, tb[IFLA_IFNAME], IFNAMSIZ);
  261. ifi = nlmsg_data(n);
  262. link->l_family = ifi->ifi_family;
  263. link->l_arptype = ifi->ifi_type;
  264. link->l_index = ifi->ifi_index;
  265. link->l_flags = ifi->ifi_flags;
  266. link->l_change = ifi->ifi_change;
  267. link->ce_mask = (LINK_ATTR_IFNAME | LINK_ATTR_FAMILY |
  268. LINK_ATTR_ARPTYPE| LINK_ATTR_IFINDEX |
  269. LINK_ATTR_FLAGS | LINK_ATTR_CHANGE);
  270. if (tb[IFLA_STATS]) {
  271. struct rtnl_link_stats *st = nla_data(tb[IFLA_STATS]);
  272. link->l_stats[RTNL_LINK_RX_PACKETS] = st->rx_packets;
  273. link->l_stats[RTNL_LINK_RX_BYTES] = st->rx_bytes;
  274. link->l_stats[RTNL_LINK_RX_ERRORS] = st->rx_errors;
  275. link->l_stats[RTNL_LINK_RX_DROPPED] = st->rx_dropped;
  276. link->l_stats[RTNL_LINK_RX_COMPRESSED] = st->rx_compressed;
  277. link->l_stats[RTNL_LINK_RX_FIFO_ERR] = st->rx_fifo_errors;
  278. link->l_stats[RTNL_LINK_TX_PACKETS] = st->tx_packets;
  279. link->l_stats[RTNL_LINK_TX_BYTES] = st->tx_bytes;
  280. link->l_stats[RTNL_LINK_TX_ERRORS] = st->tx_errors;
  281. link->l_stats[RTNL_LINK_TX_DROPPED] = st->tx_dropped;
  282. link->l_stats[RTNL_LINK_TX_COMPRESSED] = st->tx_compressed;
  283. link->l_stats[RTNL_LINK_TX_FIFO_ERR] = st->tx_fifo_errors;
  284. link->l_stats[RTNL_LINK_RX_LEN_ERR] = st->rx_length_errors;
  285. link->l_stats[RTNL_LINK_RX_OVER_ERR] = st->rx_over_errors;
  286. link->l_stats[RTNL_LINK_RX_CRC_ERR] = st->rx_crc_errors;
  287. link->l_stats[RTNL_LINK_RX_FRAME_ERR] = st->rx_frame_errors;
  288. link->l_stats[RTNL_LINK_RX_MISSED_ERR] = st->rx_missed_errors;
  289. link->l_stats[RTNL_LINK_TX_ABORT_ERR] = st->tx_aborted_errors;
  290. link->l_stats[RTNL_LINK_TX_CARRIER_ERR] = st->tx_carrier_errors;
  291. link->l_stats[RTNL_LINK_TX_HBEAT_ERR] = st->tx_heartbeat_errors;
  292. link->l_stats[RTNL_LINK_TX_WIN_ERR] = st->tx_window_errors;
  293. link->l_stats[RTNL_LINK_MULTICAST] = st->multicast;
  294. link->ce_mask |= LINK_ATTR_STATS;
  295. }
  296. if (tb[IFLA_TXQLEN]) {
  297. link->l_txqlen = nla_get_u32(tb[IFLA_TXQLEN]);
  298. link->ce_mask |= LINK_ATTR_TXQLEN;
  299. }
  300. if (tb[IFLA_MTU]) {
  301. link->l_mtu = nla_get_u32(tb[IFLA_MTU]);
  302. link->ce_mask |= LINK_ATTR_MTU;
  303. }
  304. if (tb[IFLA_ADDRESS]) {
  305. link->l_addr = nla_get_addr(tb[IFLA_ADDRESS], AF_UNSPEC);
  306. if (link->l_addr == NULL)
  307. goto errout;
  308. nl_addr_set_family(link->l_addr,
  309. nl_addr_guess_family(link->l_addr));
  310. link->ce_mask |= LINK_ATTR_ADDR;
  311. }
  312. if (tb[IFLA_BROADCAST]) {
  313. link->l_bcast = nla_get_addr(tb[IFLA_BROADCAST], AF_UNSPEC);
  314. if (link->l_bcast == NULL)
  315. goto errout;
  316. nl_addr_set_family(link->l_bcast,
  317. nl_addr_guess_family(link->l_bcast));
  318. link->ce_mask |= LINK_ATTR_BRD;
  319. }
  320. if (tb[IFLA_LINK]) {
  321. link->l_link = nla_get_u32(tb[IFLA_LINK]);
  322. link->ce_mask |= LINK_ATTR_LINK;
  323. }
  324. if (tb[IFLA_WEIGHT]) {
  325. link->l_weight = nla_get_u32(tb[IFLA_WEIGHT]);
  326. link->ce_mask |= LINK_ATTR_WEIGHT;
  327. }
  328. if (tb[IFLA_QDISC]) {
  329. nla_strlcpy(link->l_qdisc, tb[IFLA_QDISC], IFQDISCSIZ);
  330. link->ce_mask |= LINK_ATTR_QDISC;
  331. }
  332. if (tb[IFLA_MAP]) {
  333. nla_memcpy(&link->l_map, tb[IFLA_MAP],
  334. sizeof(struct rtnl_link_ifmap));
  335. link->ce_mask |= LINK_ATTR_MAP;
  336. }
  337. if (tb[IFLA_MASTER]) {
  338. link->l_master = nla_get_u32(tb[IFLA_MASTER]);
  339. link->ce_mask |= LINK_ATTR_MASTER;
  340. }
  341. if (tb[IFLA_OPERSTATE]) {
  342. link->l_operstate = nla_get_u8(tb[IFLA_OPERSTATE]);
  343. link->ce_mask |= LINK_ATTR_OPERSTATE;
  344. }
  345. if (tb[IFLA_LINKMODE]) {
  346. link->l_linkmode = nla_get_u8(tb[IFLA_LINKMODE]);
  347. link->ce_mask |= LINK_ATTR_LINKMODE;
  348. }
  349. if (tb[IFLA_LINKINFO]) {
  350. struct nlattr *li[IFLA_INFO_MAX+1];
  351. err = nla_parse_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
  352. link_info_policy);
  353. if (err < 0)
  354. goto errout;
  355. if (li[IFLA_INFO_KIND] &&
  356. (li[IFLA_INFO_DATA] || li[IFLA_INFO_XSTATS])) {
  357. struct rtnl_link_info_ops *ops;
  358. char *kind;
  359. kind = nla_get_string(li[IFLA_INFO_KIND]);
  360. ops = rtnl_link_info_ops_lookup(kind);
  361. if (ops != NULL) {
  362. ops->io_refcnt++;
  363. link->l_info_ops = ops;
  364. err = ops->io_parse(link, li[IFLA_INFO_DATA],
  365. li[IFLA_INFO_XSTATS]);
  366. if (err < 0)
  367. goto errout;
  368. } else {
  369. /* XXX: Warn about unparsed info? */
  370. }
  371. }
  372. }
  373. err = pp->pp_cb((struct nl_object *) link, pp);
  374. if (err < 0)
  375. goto errout;
  376. err = P_ACCEPT;
  377. errout:
  378. rtnl_link_put(link);
  379. return err;
  380. }
  381. static int link_request_update(struct nl_cache *c, struct nl_handle *h)
  382. {
  383. return nl_rtgen_request(h, RTM_GETLINK, AF_UNSPEC, NLM_F_DUMP);
  384. }
  385. static int link_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
  386. {
  387. char buf[128];
  388. struct nl_cache *cache = dp_cache(obj);
  389. struct rtnl_link *link = (struct rtnl_link *) obj;
  390. int line = 1;
  391. dp_dump(p, "%s %s ", link->l_name,
  392. nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
  393. if (link->l_addr && !nl_addr_iszero(link->l_addr))
  394. dp_dump(p, "%s ", nl_addr2str(link->l_addr, buf, sizeof(buf)));
  395. if (link->ce_mask & LINK_ATTR_MASTER) {
  396. struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
  397. dp_dump(p, "master %s ", master ? master->l_name : "inv");
  398. if (master)
  399. rtnl_link_put(master);
  400. }
  401. rtnl_link_flags2str(link->l_flags, buf, sizeof(buf));
  402. if (buf[0])
  403. dp_dump(p, "<%s> ", buf);
  404. if (link->ce_mask & LINK_ATTR_LINK) {
  405. struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
  406. dp_dump(p, "slave-of %s ", ll ? ll->l_name : "NONE");
  407. if (ll)
  408. rtnl_link_put(ll);
  409. }
  410. if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_BRIEF])
  411. line = link->l_info_ops->io_dump[NL_DUMP_BRIEF](link, p, line);
  412. dp_dump(p, "\n");
  413. return line;
  414. }
  415. static int link_dump_full(struct nl_object *obj, struct nl_dump_params *p)
  416. {
  417. struct rtnl_link *link = (struct rtnl_link *) obj;
  418. char buf[64];
  419. int line;
  420. line = link_dump_brief(obj, p);
  421. dp_new_line(p, line++);
  422. dp_dump(p, " mtu %u ", link->l_mtu);
  423. dp_dump(p, "txqlen %u weight %u ", link->l_txqlen, link->l_weight);
  424. if (link->ce_mask & LINK_ATTR_QDISC)
  425. dp_dump(p, "qdisc %s ", link->l_qdisc);
  426. if (link->ce_mask & LINK_ATTR_MAP && link->l_map.lm_irq)
  427. dp_dump(p, "irq %u ", link->l_map.lm_irq);
  428. if (link->ce_mask & LINK_ATTR_IFINDEX)
  429. dp_dump(p, "index %u ", link->l_index);
  430. dp_dump(p, "\n");
  431. dp_new_line(p, line++);
  432. dp_dump(p, " ");
  433. if (link->ce_mask & LINK_ATTR_BRD)
  434. dp_dump(p, "brd %s ", nl_addr2str(link->l_bcast, buf,
  435. sizeof(buf)));
  436. if ((link->ce_mask & LINK_ATTR_OPERSTATE) &&
  437. link->l_operstate != IF_OPER_UNKNOWN) {
  438. rtnl_link_operstate2str(link->l_operstate, buf, sizeof(buf));
  439. dp_dump(p, "state %s ", buf);
  440. }
  441. dp_dump(p, "mode %s\n",
  442. rtnl_link_mode2str(link->l_linkmode, buf, sizeof(buf)));
  443. if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_FULL])
  444. line = link->l_info_ops->io_dump[NL_DUMP_FULL](link, p, line);
  445. return line;
  446. }
  447. static int link_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
  448. {
  449. struct rtnl_link *link = (struct rtnl_link *) obj;
  450. char *unit, fmt[64];
  451. float res;
  452. int line;
  453. line = link_dump_full(obj, p);
  454. dp_dump_line(p, line++, " Stats: bytes packets errors "
  455. " dropped fifo-err compressed\n");
  456. res = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_RX_BYTES], &unit);
  457. strcpy(fmt, " RX %X.2f %s %10llu %10llu %10llu %10llu %10llu\n");
  458. fmt[9] = *unit == 'B' ? '9' : '7';
  459. dp_dump_line(p, line++, fmt,
  460. res, unit,
  461. link->l_stats[RTNL_LINK_RX_PACKETS],
  462. link->l_stats[RTNL_LINK_RX_ERRORS],
  463. link->l_stats[RTNL_LINK_RX_DROPPED],
  464. link->l_stats[RTNL_LINK_RX_FIFO_ERR],
  465. link->l_stats[RTNL_LINK_RX_COMPRESSED]);
  466. res = nl_cancel_down_bytes(link->l_stats[RTNL_LINK_TX_BYTES], &unit);
  467. strcpy(fmt, " TX %X.2f %s %10llu %10llu %10llu %10llu %10llu\n");
  468. fmt[9] = *unit == 'B' ? '9' : '7';
  469. dp_dump_line(p, line++, fmt,
  470. res, unit,
  471. link->l_stats[RTNL_LINK_TX_PACKETS],
  472. link->l_stats[RTNL_LINK_TX_ERRORS],
  473. link->l_stats[RTNL_LINK_TX_DROPPED],
  474. link->l_stats[RTNL_LINK_TX_FIFO_ERR],
  475. link->l_stats[RTNL_LINK_TX_COMPRESSED]);
  476. dp_dump_line(p, line++, " Errors: length over crc "
  477. " frame missed multicast\n");
  478. dp_dump_line(p, line++, " RX %10" PRIu64 " %10" PRIu64 " %10"
  479. PRIu64 " %10" PRIu64 " %10" PRIu64 " %10"
  480. PRIu64 "\n",
  481. link->l_stats[RTNL_LINK_RX_LEN_ERR],
  482. link->l_stats[RTNL_LINK_RX_OVER_ERR],
  483. link->l_stats[RTNL_LINK_RX_CRC_ERR],
  484. link->l_stats[RTNL_LINK_RX_FRAME_ERR],
  485. link->l_stats[RTNL_LINK_RX_MISSED_ERR],
  486. link->l_stats[RTNL_LINK_MULTICAST]);
  487. dp_dump_line(p, line++, " Errors: aborted carrier heartbeat "
  488. " window collision\n");
  489. dp_dump_line(p, line++, " TX %10" PRIu64 " %10" PRIu64 " %10"
  490. PRIu64 " %10" PRIu64 " %10" PRIu64 "\n",
  491. link->l_stats[RTNL_LINK_TX_ABORT_ERR],
  492. link->l_stats[RTNL_LINK_TX_CARRIER_ERR],
  493. link->l_stats[RTNL_LINK_TX_HBEAT_ERR],
  494. link->l_stats[RTNL_LINK_TX_WIN_ERR],
  495. link->l_stats[RTNL_LINK_TX_COLLISIONS]);
  496. if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_STATS])
  497. line = link->l_info_ops->io_dump[NL_DUMP_STATS](link, p, line);
  498. return line;
  499. }
  500. static int link_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
  501. {
  502. struct rtnl_link *link = (struct rtnl_link *) obj;
  503. struct nl_cache *cache = dp_cache(obj);
  504. char buf[128];
  505. int i, line = 0;
  506. dp_dump_line(p, line++, "<link name=\"%s\" index=\"%u\">\n",
  507. link->l_name, link->l_index);
  508. dp_dump_line(p, line++, " <family>%s</family>\n",
  509. nl_af2str(link->l_family, buf, sizeof(buf)));
  510. dp_dump_line(p, line++, " <arptype>%s</arptype>\n",
  511. nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
  512. dp_dump_line(p, line++, " <address>%s</address>\n",
  513. nl_addr2str(link->l_addr, buf, sizeof(buf)));
  514. dp_dump_line(p, line++, " <mtu>%u</mtu>\n", link->l_mtu);
  515. dp_dump_line(p, line++, " <txqlen>%u</txqlen>\n", link->l_txqlen);
  516. dp_dump_line(p, line++, " <weight>%u</weight>\n", link->l_weight);
  517. rtnl_link_flags2str(link->l_flags, buf, sizeof(buf));
  518. if (buf[0])
  519. dp_dump_line(p, line++, " <flags>%s</flags>\n", buf);
  520. if (link->ce_mask & LINK_ATTR_QDISC)
  521. dp_dump_line(p, line++, " <qdisc>%s</qdisc>\n", link->l_qdisc);
  522. if (link->ce_mask & LINK_ATTR_LINK) {
  523. struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
  524. dp_dump_line(p, line++, " <link>%s</link>\n",
  525. ll ? ll->l_name : "none");
  526. if (ll)
  527. rtnl_link_put(ll);
  528. }
  529. if (link->ce_mask & LINK_ATTR_MASTER) {
  530. struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
  531. dp_dump_line(p, line++, " <master>%s</master>\n",
  532. master ? master->l_name : "none");
  533. if (master)
  534. rtnl_link_put(master);
  535. }
  536. if (link->ce_mask & LINK_ATTR_BRD)
  537. dp_dump_line(p, line++, " <broadcast>%s</broadcast>\n",
  538. nl_addr2str(link->l_bcast, buf, sizeof(buf)));
  539. if (link->ce_mask & LINK_ATTR_STATS) {
  540. dp_dump_line(p, line++, " <stats>\n");
  541. for (i = 0; i <= RTNL_LINK_STATS_MAX; i++) {
  542. rtnl_link_stat2str(i, buf, sizeof(buf));
  543. dp_dump_line(p, line++,
  544. " <%s>%" PRIu64 "</%s>\n",
  545. buf, link->l_stats[i], buf);
  546. }
  547. dp_dump_line(p, line++, " </stats>\n");
  548. }
  549. if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_XML]) {
  550. dp_dump_line(p, line++, " <info>\n");
  551. line = link->l_info_ops->io_dump[NL_DUMP_XML](link, p, line);
  552. dp_dump_line(p, line++, " </info>\n");
  553. }
  554. dp_dump_line(p, line++, "</link>\n");
  555. #if 0
  556. uint32_t l_change; /**< Change mask */
  557. struct rtnl_lifmap l_map; /**< Interface device mapping */
  558. #endif
  559. return line;
  560. }
  561. static int link_dump_env(struct nl_object *obj, struct nl_dump_params *p)
  562. {
  563. struct rtnl_link *link = (struct rtnl_link *) obj;
  564. struct nl_cache *cache = dp_cache(obj);
  565. char buf[128];
  566. int i, line = 0;
  567. dp_dump_line(p, line++, "LINK_NAME=%s\n", link->l_name);
  568. dp_dump_line(p, line++, "LINK_IFINDEX=%u\n", link->l_index);
  569. dp_dump_line(p, line++, "LINK_FAMILY=%s\n",
  570. nl_af2str(link->l_family, buf, sizeof(buf)));
  571. dp_dump_line(p, line++, "LINK_TYPE=%s\n",
  572. nl_llproto2str(link->l_arptype, buf, sizeof(buf)));
  573. if (link->ce_mask & LINK_ATTR_ADDR)
  574. dp_dump_line(p, line++, "LINK_ADDRESS=%s\n",
  575. nl_addr2str(link->l_addr, buf, sizeof(buf)));
  576. dp_dump_line(p, line++, "LINK_MTU=%u\n", link->l_mtu);
  577. dp_dump_line(p, line++, "LINK_TXQUEUELEN=%u\n", link->l_txqlen);
  578. dp_dump_line(p, line++, "LINK_WEIGHT=%u\n", link->l_weight);
  579. rtnl_link_flags2str(link->l_flags & ~IFF_RUNNING, buf, sizeof(buf));
  580. if (buf[0])
  581. dp_dump_line(p, line++, "LINK_FLAGS=%s\n", buf);
  582. if (link->ce_mask & LINK_ATTR_QDISC)
  583. dp_dump_line(p, line++, "LINK_QDISC=%s\n", link->l_qdisc);
  584. if (link->ce_mask & LINK_ATTR_LINK) {
  585. struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
  586. dp_dump_line(p, line++, "LINK_LINK_IFINDEX=%d\n", link->l_link);
  587. if (ll) {
  588. dp_dump_line(p, line++, "LINK_LINK_IFNAME=%s\n",
  589. ll->l_name);
  590. rtnl_link_put(ll);
  591. }
  592. }
  593. if (link->ce_mask & LINK_ATTR_MASTER) {
  594. struct rtnl_link *master = rtnl_link_get(cache, link->l_master);
  595. dp_dump_line(p, line++, "LINK_MASTER=%s\n",
  596. master ? master->l_name : "none");
  597. if (master)
  598. rtnl_link_put(master);
  599. }
  600. if (link->ce_mask & LINK_ATTR_BRD)
  601. dp_dump_line(p, line++, "LINK_BROADCAST=%s\n",
  602. nl_addr2str(link->l_bcast, buf, sizeof(buf)));
  603. if (link->ce_mask & LINK_ATTR_STATS) {
  604. for (i = 0; i <= RTNL_LINK_STATS_MAX; i++) {
  605. char *c = buf;
  606. sprintf(buf, "LINK_");
  607. rtnl_link_stat2str(i, buf + 5, sizeof(buf) - 5);
  608. while (*c) {
  609. *c = toupper(*c);
  610. c++;
  611. }
  612. dp_dump_line(p, line++,
  613. "%s=%" PRIu64 "\n", buf, link->l_stats[i]);
  614. }
  615. }
  616. if (link->l_info_ops && link->l_info_ops->io_dump[NL_DUMP_ENV])
  617. line = link->l_info_ops->io_dump[NL_DUMP_ENV](link, p, line);
  618. return line;
  619. }
  620. #if 0
  621. static int link_handle_event(struct nl_object *a, struct rtnl_link_event_cb *cb)
  622. {
  623. struct rtnl_link *l = (struct rtnl_link *) a;
  624. struct nl_cache *c = dp_cache(a);
  625. int nevents = 0;
  626. if (l->l_change == ~0U) {
  627. if (l->ce_msgtype == RTM_NEWLINK)
  628. cb->le_register(l);
  629. else
  630. cb->le_unregister(l);
  631. return 1;
  632. }
  633. if (l->l_change & IFF_SLAVE) {
  634. if (l->l_flags & IFF_SLAVE) {
  635. struct rtnl_link *m = rtnl_link_get(c, l->l_master);
  636. cb->le_new_bonding(l, m);
  637. if (m)
  638. rtnl_link_put(m);
  639. } else
  640. cb->le_cancel_bonding(l);
  641. }
  642. #if 0
  643. if (l->l_change & IFF_UP && l->l_change & IFF_RUNNING)
  644. dp_dump_line(p, line++, "link %s changed state to %s.\n",
  645. l->l_name, l->l_flags & IFF_UP ? "up" : "down");
  646. if (l->l_change & IFF_PROMISC) {
  647. dp_new_line(p, line++);
  648. dp_dump(p, "link %s %s promiscuous mode.\n",
  649. l->l_name, l->l_flags & IFF_PROMISC ? "entered" : "left");
  650. }
  651. if (line == 0)
  652. dp_dump_line(p, line++, "link %s sent unknown event.\n",
  653. l->l_name);
  654. #endif
  655. return nevents;
  656. }
  657. #endif
  658. static int link_compare(struct nl_object *_a, struct nl_object *_b,
  659. uint32_t attrs, int flags)
  660. {
  661. struct rtnl_link *a = (struct rtnl_link *) _a;
  662. struct rtnl_link *b = (struct rtnl_link *) _b;
  663. int diff = 0;
  664. #define LINK_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, LINK_ATTR_##ATTR, a, b, EXPR)
  665. diff |= LINK_DIFF(IFINDEX, a->l_index != b->l_index);
  666. diff |= LINK_DIFF(MTU, a->l_mtu != b->l_mtu);
  667. diff |= LINK_DIFF(LINK, a->l_link != b->l_link);
  668. diff |= LINK_DIFF(TXQLEN, a->l_txqlen != b->l_txqlen);
  669. diff |= LINK_DIFF(WEIGHT, a->l_weight != b->l_weight);
  670. diff |= LINK_DIFF(MASTER, a->l_master != b->l_master);
  671. diff |= LINK_DIFF(FAMILY, a->l_family != b->l_family);
  672. diff |= LINK_DIFF(OPERSTATE, a->l_operstate != b->l_operstate);
  673. diff |= LINK_DIFF(LINKMODE, a->l_linkmode != b->l_linkmode);
  674. diff |= LINK_DIFF(QDISC, strcmp(a->l_qdisc, b->l_qdisc));
  675. diff |= LINK_DIFF(IFNAME, strcmp(a->l_name, b->l_name));
  676. diff |= LINK_DIFF(ADDR, nl_addr_cmp(a->l_addr, b->l_addr));
  677. diff |= LINK_DIFF(BRD, nl_addr_cmp(a->l_bcast, b->l_bcast));
  678. if (flags & LOOSE_FLAG_COMPARISON)
  679. diff |= LINK_DIFF(FLAGS,
  680. (a->l_flags ^ b->l_flags) & b->l_flag_mask);
  681. else
  682. diff |= LINK_DIFF(FLAGS, a->l_flags != b->l_flags);
  683. #undef LINK_DIFF
  684. return diff;
  685. }
  686. static struct trans_tbl link_attrs[] = {
  687. __ADD(LINK_ATTR_MTU, mtu)
  688. __ADD(LINK_ATTR_LINK, link)
  689. __ADD(LINK_ATTR_TXQLEN, txqlen)
  690. __ADD(LINK_ATTR_WEIGHT, weight)
  691. __ADD(LINK_ATTR_MASTER, master)
  692. __ADD(LINK_ATTR_QDISC, qdisc)
  693. __ADD(LINK_ATTR_MAP, map)
  694. __ADD(LINK_ATTR_ADDR, address)
  695. __ADD(LINK_ATTR_BRD, broadcast)
  696. __ADD(LINK_ATTR_FLAGS, flags)
  697. __ADD(LINK_ATTR_IFNAME, name)
  698. __ADD(LINK_ATTR_IFINDEX, ifindex)
  699. __ADD(LINK_ATTR_FAMILY, family)
  700. __ADD(LINK_ATTR_ARPTYPE, arptype)
  701. __ADD(LINK_ATTR_STATS, stats)
  702. __ADD(LINK_ATTR_CHANGE, change)
  703. __ADD(LINK_ATTR_OPERSTATE, operstate)
  704. __ADD(LINK_ATTR_LINKMODE, linkmode)
  705. };
  706. static char *link_attrs2str(int attrs, char *buf, size_t len)
  707. {
  708. return __flags2str(attrs, buf, len, link_attrs,
  709. ARRAY_SIZE(link_attrs));
  710. }
  711. /**
  712. * @name Allocation/Freeing
  713. * @{
  714. */
  715. struct rtnl_link *rtnl_link_alloc(void)
  716. {
  717. return (struct rtnl_link *) nl_object_alloc(&link_obj_ops);
  718. }
  719. void rtnl_link_put(struct rtnl_link *link)
  720. {
  721. nl_object_put((struct nl_object *) link);
  722. }
  723. /** @} */
  724. /**
  725. * @name Cache Management
  726. * @{
  727. */
  728. /**
  729. * Allocate link cache and fill in all configured links.
  730. * @arg handle Netlink handle.
  731. *
  732. * Allocates a new link cache, initializes it properly and updates it
  733. * to include all links currently configured in the kernel.
  734. *
  735. * @note Free the memory after usage.
  736. * @return Newly allocated cache or NULL if an error occured.
  737. */
  738. struct nl_cache *rtnl_link_alloc_cache(struct nl_handle *handle)
  739. {
  740. struct nl_cache * cache;
  741. cache = nl_cache_alloc(&rtnl_link_ops);
  742. if (cache == NULL)
  743. return NULL;
  744. if (handle && nl_cache_refill(handle, cache) < 0) {
  745. nl_cache_free(cache);
  746. return NULL;
  747. }
  748. return cache;
  749. }
  750. /**
  751. * Look up link by interface index in the provided cache
  752. * @arg cache link cache
  753. * @arg ifindex link interface index
  754. *
  755. * The caller owns a reference on the returned object and
  756. * must give the object back via rtnl_link_put().
  757. *
  758. * @return pointer to link inside the cache or NULL if no match was found.
  759. */
  760. struct rtnl_link *rtnl_link_get(struct nl_cache *cache, int ifindex)
  761. {
  762. struct rtnl_link *link;
  763. if (cache->c_ops != &rtnl_link_ops)
  764. return NULL;
  765. nl_list_for_each_entry(link, &cache->c_items, ce_list) {
  766. if (link->l_index == ifindex) {
  767. nl_object_get((struct nl_object *) link);
  768. return link;
  769. }
  770. }
  771. return NULL;
  772. }
  773. /**
  774. * Look up link by link name in the provided cache
  775. * @arg cache link cache
  776. * @arg name link name
  777. *
  778. * The caller owns a reference on the returned object and
  779. * must give the object back via rtnl_link_put().
  780. *
  781. * @return pointer to link inside the cache or NULL if no match was found.
  782. */
  783. struct rtnl_link *rtnl_link_get_by_name(struct nl_cache *cache,
  784. const char *name)
  785. {
  786. struct rtnl_link *link;
  787. if (cache->c_ops != &rtnl_link_ops)
  788. return NULL;
  789. nl_list_for_each_entry(link, &cache->c_items, ce_list) {
  790. if (!strcmp(name, link->l_name)) {
  791. nl_object_get((struct nl_object *) link);
  792. return link;
  793. }
  794. }
  795. return NULL;
  796. }
  797. /** @} */
  798. /**
  799. * @name Link Modifications
  800. * @{
  801. */
  802. /**
  803. * Builds a netlink change request message to change link attributes
  804. * @arg old link to be changed
  805. * @arg tmpl template with requested changes
  806. * @arg flags additional netlink message flags
  807. *
  808. * Builds a new netlink message requesting a change of link attributes.
  809. * The netlink message header isn't fully equipped with all relevant
  810. * fields and must be sent out via nl_send_auto_complete() or
  811. * supplemented as needed.
  812. * \a old must point to a link currently configured in the kernel
  813. * and \a tmpl must contain the attributes to be changed set via
  814. * \c rtnl_link_set_* functions.
  815. *
  816. * @return New netlink message
  817. * @note Not all attributes can be changed, see
  818. * \ref link_changeable "Changeable Attributes" for more details.
  819. */
  820. struct nl_msg * rtnl_link_build_change_request(struct rtnl_link *old,
  821. struct rtnl_link *tmpl,
  822. int flags)
  823. {
  824. struct nl_msg *msg;
  825. struct ifinfomsg ifi = {
  826. .ifi_family = old->l_family,
  827. .ifi_index = old->l_index,
  828. };
  829. if (tmpl->ce_mask & LINK_ATTR_FLAGS) {
  830. ifi.ifi_flags = old->l_flags & ~tmpl->l_flag_mask;
  831. ifi.ifi_flags |= tmpl->l_flags;
  832. }
  833. msg = nlmsg_alloc_simple(RTM_SETLINK, flags);
  834. if (!msg)
  835. goto nla_put_failure;
  836. if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0)
  837. goto nla_put_failure;
  838. if (tmpl->ce_mask & LINK_ATTR_ADDR)
  839. NLA_PUT_ADDR(msg, IFLA_ADDRESS, tmpl->l_addr);
  840. if (tmpl->ce_mask & LINK_ATTR_BRD)
  841. NLA_PUT_ADDR(msg, IFLA_BROADCAST, tmpl->l_bcast);
  842. if (tmpl->ce_mask & LINK_ATTR_MTU)
  843. NLA_PUT_U32(msg, IFLA_MTU, tmpl->l_mtu);
  844. if (tmpl->ce_mask & LINK_ATTR_TXQLEN)
  845. NLA_PUT_U32(msg, IFLA_TXQLEN, tmpl->l_txqlen);
  846. if (tmpl->ce_mask & LINK_ATTR_WEIGHT)
  847. NLA_PUT_U32(msg, IFLA_WEIGHT, tmpl->l_weight);
  848. if (tmpl->ce_mask & LINK_ATTR_IFNAME)
  849. NLA_PUT_STRING(msg, IFLA_IFNAME, tmpl->l_name);
  850. if (tmpl->ce_mask & LINK_ATTR_OPERSTATE)
  851. NLA_PUT_U8(msg, IFLA_OPERSTATE, tmpl->l_operstate);
  852. if (tmpl->ce_mask & LINK_ATTR_LINKMODE)
  853. NLA_PUT_U8(msg, IFLA_LINKMODE, tmpl->l_linkmode);
  854. if ((tmpl->ce_mask & LINK_ATTR_LINKINFO) && tmpl->l_info_ops &&
  855. tmpl->l_info_ops->io_put_attrs) {
  856. struct nlattr *info;
  857. if (!(info = nla_nest_start(msg, IFLA_LINKINFO)))
  858. goto nla_put_failure;
  859. NLA_PUT_STRING(msg, IFLA_INFO_KIND, tmpl->l_info_ops->io_name);
  860. if (tmpl->l_info_ops->io_put_attrs(msg, tmpl) < 0)
  861. goto nla_put_failure;
  862. nla_nest_end(msg, info);
  863. }
  864. return msg;
  865. nla_put_failure:
  866. nlmsg_free(msg);
  867. return NULL;
  868. }
  869. /**
  870. * Change link attributes
  871. * @arg handle netlink handle
  872. * @arg old link to be changed
  873. * @arg tmpl template with requested changes
  874. * @arg flags additional netlink message flags
  875. *
  876. * Builds a new netlink message by calling rtnl_link_build_change_request(),
  877. * sends the request to the kernel and waits for the next ACK to be
  878. * received, i.e. blocks until the request has been processed.
  879. *
  880. * @return 0 on success or a negative error code
  881. * @note Not all attributes can be changed, see
  882. * \ref link_changeable "Changeable Attributes" for more details.
  883. */
  884. int rtnl_link_change(struct nl_handle *handle, struct rtnl_link *old,
  885. struct rtnl_link *tmpl, int flags)
  886. {
  887. int err;
  888. struct nl_msg *msg;
  889. msg = rtnl_link_build_change_request(old, tmpl, flags);
  890. if (!msg)
  891. return nl_errno(ENOMEM);
  892. err = nl_send_auto_complete(handle, msg);
  893. nlmsg_free(msg);
  894. if (err < 0)
  895. return err;
  896. return nl_wait_for_ack(handle);
  897. }
  898. /** @} */
  899. /**
  900. * @name Name <-> Index Translations
  901. * @{
  902. */
  903. /**
  904. * Translate an interface index to the corresponding link name
  905. * @arg cache link cache
  906. * @arg ifindex link interface index
  907. * @arg dst destination buffer
  908. * @arg len length of destination buffer
  909. *
  910. * Translates the specified interface index to the corresponding
  911. * link name and stores the name in the destination buffer.
  912. *
  913. * @return link name or NULL if no match was found.
  914. */
  915. char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst,
  916. size_t len)
  917. {
  918. struct rtnl_link *link = rtnl_link_get(cache, ifindex);
  919. if (link) {
  920. strncpy(dst, link->l_name, len - 1);
  921. rtnl_link_put(link);
  922. return dst;
  923. }
  924. return NULL;
  925. }
  926. /**
  927. * Translate a link name to the corresponding interface index
  928. * @arg cache link cache
  929. * @arg name link name
  930. *
  931. * @return interface index or RTNL_LINK_NOT_FOUND if no match was found.
  932. */
  933. int rtnl_link_name2i(struct nl_cache *cache, const char *name)
  934. {
  935. int ifindex = RTNL_LINK_NOT_FOUND;
  936. struct rtnl_link *link;
  937. link = rtnl_link_get_by_name(cache, name);
  938. if (link) {
  939. ifindex = link->l_index;
  940. rtnl_link_put(link);
  941. }
  942. return ifindex;
  943. }
  944. /** @} */
  945. /**
  946. * @name Link Flags Translations
  947. * @{
  948. */
  949. static struct trans_tbl link_flags[] = {
  950. __ADD(IFF_LOOPBACK, loopback)
  951. __ADD(IFF_BROADCAST, broadcast)
  952. __ADD(IFF_POINTOPOINT, pointopoint)
  953. __ADD(IFF_MULTICAST, multicast)
  954. __ADD(IFF_NOARP, noarp)
  955. __ADD(IFF_ALLMULTI, allmulti)
  956. __ADD(IFF_PROMISC, promisc)
  957. __ADD(IFF_MASTER, master)
  958. __ADD(IFF_SLAVE, slave)
  959. __ADD(IFF_DEBUG, debug)
  960. __ADD(IFF_DYNAMIC, dynamic)
  961. __ADD(IFF_AUTOMEDIA, automedia)
  962. __ADD(IFF_PORTSEL, portsel)
  963. __ADD(IFF_NOTRAILERS, notrailers)
  964. __ADD(IFF_UP, up)
  965. __ADD(IFF_RUNNING, running)
  966. __ADD(IFF_LOWER_UP, lowerup)
  967. __ADD(IFF_DORMANT, dormant)
  968. __ADD(IFF_ECHO, echo)
  969. };
  970. char * rtnl_link_flags2str(int flags, char *buf, size_t len)
  971. {
  972. return __flags2str(flags, buf, len, link_flags,
  973. ARRAY_SIZE(link_flags));
  974. }
  975. int rtnl_link_str2flags(const char *name)
  976. {
  977. return __str2flags(name, link_flags, ARRAY_SIZE(link_flags));
  978. }
  979. /** @} */
  980. /**
  981. * @name Link Statistics Translations
  982. * @{
  983. */
  984. static struct trans_tbl link_stats[] = {
  985. __ADD(RTNL_LINK_RX_PACKETS, rx_packets)
  986. __ADD(RTNL_LINK_TX_PACKETS, tx_packets)
  987. __ADD(RTNL_LINK_RX_BYTES, rx_bytes)
  988. __ADD(RTNL_LINK_TX_BYTES, tx_bytes)
  989. __ADD(RTNL_LINK_RX_ERRORS, rx_errors)
  990. __ADD(RTNL_LINK_TX_ERRORS, tx_errors)
  991. __ADD(RTNL_LINK_RX_DROPPED, rx_dropped)
  992. __ADD(RTNL_LINK_TX_DROPPED, tx_dropped)
  993. __ADD(RTNL_LINK_RX_COMPRESSED, rx_compressed)
  994. __ADD(RTNL_LINK_TX_COMPRESSED, tx_compressed)
  995. __ADD(RTNL_LINK_RX_FIFO_ERR, rx_fifo_err)
  996. __ADD(RTNL_LINK_TX_FIFO_ERR, tx_fifo_err)
  997. __ADD(RTNL_LINK_RX_LEN_ERR, rx_len_err)
  998. __ADD(RTNL_LINK_RX_OVER_ERR, rx_over_err)
  999. __ADD(RTNL_LINK_RX_CRC_ERR, rx_crc_err)
  1000. __ADD(RTNL_LINK_RX_FRAME_ERR, rx_frame_err)
  1001. __ADD(RTNL_LINK_RX_MISSED_ERR, rx_missed_err)
  1002. __ADD(RTNL_LINK_TX_ABORT_ERR, tx_abort_err)
  1003. __ADD(RTNL_LINK_TX_CARRIER_ERR, tx_carrier_err)
  1004. __ADD(RTNL_LINK_TX_HBEAT_ERR, tx_hbeat_err)
  1005. __ADD(RTNL_LINK_TX_WIN_ERR, tx_win_err)
  1006. __ADD(RTNL_LINK_TX_COLLISIONS, tx_collision)
  1007. __ADD(RTNL_LINK_MULTICAST, multicast)
  1008. };
  1009. char *rtnl_link_stat2str(int st, char *buf, size_t len)
  1010. {
  1011. return __type2str(st, buf, len, link_stats, ARRAY_SIZE(link_stats));
  1012. }
  1013. int rtnl_link_str2stat(const char *name)
  1014. {
  1015. return __str2type(name, link_stats, ARRAY_SIZE(link_stats));
  1016. }
  1017. /** @} */
  1018. /**
  1019. * @name Link Operstate Translations
  1020. * @{
  1021. */
  1022. static struct trans_tbl link_operstates[] = {
  1023. __ADD(IF_OPER_UNKNOWN, unknown)
  1024. __ADD(IF_OPER_NOTPRESENT, notpresent)
  1025. __ADD(IF_OPER_DOWN, down)
  1026. __ADD(IF_OPER_LOWERLAYERDOWN, lowerlayerdown)
  1027. __ADD(IF_OPER_TESTING, testing)
  1028. __ADD(IF_OPER_DORMANT, dormant)
  1029. __ADD(IF_OPER_UP, up)
  1030. };
  1031. char *rtnl_link_operstate2str(int st, char *buf, size_t len)
  1032. {
  1033. return __type2str(st, buf, len, link_operstates,
  1034. ARRAY_SIZE(link_operstates));
  1035. }
  1036. int rtnl_link_str2operstate(const char *name)
  1037. {
  1038. return __str2type(name, link_operstates,
  1039. ARRAY_SIZE(link_operstates));
  1040. }
  1041. /** @} */
  1042. /**
  1043. * @name Link Mode Translations
  1044. * @{
  1045. */
  1046. static struct trans_tbl link_modes[] = {
  1047. __ADD(IF_LINK_MODE_DEFAULT, default)
  1048. __ADD(IF_LINK_MODE_DORMANT, dormant)
  1049. };
  1050. char *rtnl_link_mode2str(int st, char *buf, size_t len)
  1051. {
  1052. return __type2str(st, buf, len, link_modes, ARRAY_SIZE(link_modes));
  1053. }
  1054. int rtnl_link_str2mode(const char *name)
  1055. {
  1056. return __str2type(name, link_modes, ARRAY_SIZE(link_modes));
  1057. }
  1058. /** @} */
  1059. /**
  1060. * @name Attributes
  1061. * @{
  1062. */
  1063. void rtnl_link_set_qdisc(struct rtnl_link *link, const char *qdisc)
  1064. {
  1065. strncpy(link->l_qdisc, qdisc, sizeof(link->l_qdisc) - 1);
  1066. link->ce_mask |= LINK_ATTR_QDISC;
  1067. }
  1068. char *rtnl_link_get_qdisc(struct rtnl_link *link)
  1069. {
  1070. if (link->ce_mask & LINK_ATTR_QDISC)
  1071. return link->l_qdisc;
  1072. else
  1073. return NULL;
  1074. }
  1075. void rtnl_link_set_name(struct rtnl_link *link, const char *name)
  1076. {
  1077. strncpy(link->l_name, name, sizeof(link->l_name) - 1);
  1078. link->ce_mask |= LINK_ATTR_IFNAME;
  1079. }
  1080. char *rtnl_link_get_name(struct rtnl_link *link)
  1081. {
  1082. if (link->ce_mask & LINK_ATTR_IFNAME)
  1083. return link->l_name;
  1084. else
  1085. return NULL;
  1086. }
  1087. static inline void __assign_addr(struct rtnl_link *link, struct nl_addr **pos,
  1088. struct nl_addr *new, int flag)
  1089. {
  1090. if (*pos)
  1091. nl_addr_put(*pos);
  1092. nl_addr_get(new);
  1093. *pos = new;
  1094. link->ce_mask |= flag;
  1095. }
  1096. void rtnl_link_set_addr(struct rtnl_link *link, struct nl_addr *addr)
  1097. {
  1098. __assign_addr(link, &link->l_addr, addr, LINK_ATTR_ADDR);
  1099. }
  1100. struct nl_addr *rtnl_link_get_addr(struct rtnl_link *link)
  1101. {
  1102. if (link->ce_mask & LINK_ATTR_ADDR)
  1103. return link->l_addr;
  1104. else
  1105. return NULL;
  1106. }
  1107. void rtnl_link_set_broadcast(struct rtnl_link *link, struct nl_addr *brd)
  1108. {
  1109. __assign_addr(link, &link->l_bcast, brd, LINK_ATTR_BRD);
  1110. }
  1111. struct nl_addr *rtnl_link_get_broadcast(struct rtnl_link *link)
  1112. {
  1113. if (link->ce_mask & LINK_ATTR_BRD)
  1114. return link->l_bcast;
  1115. else
  1116. return NULL;
  1117. }
  1118. void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags)
  1119. {
  1120. link->l_flag_mask |= flags;
  1121. link->l_flags |= flags;
  1122. link->ce_mask |= LINK_ATTR_FLAGS;
  1123. }
  1124. void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags)
  1125. {
  1126. link->l_flag_mask |= flags;
  1127. link->l_flags &= ~flags;
  1128. link->ce_mask |= LINK_ATTR_FLAGS;
  1129. }
  1130. unsigned int rtnl_link_get_flags(struct rtnl_link *link)
  1131. {
  1132. return link->l_flags;
  1133. }
  1134. void rtnl_link_set_family(struct rtnl_link *link, int family)
  1135. {
  1136. link->l_family = family;
  1137. link->ce_mask |= LINK_ATTR_FAMILY;
  1138. }
  1139. int rtnl_link_get_family(struct rtnl_link *link)
  1140. {
  1141. if (link->l_family & LINK_ATTR_FAMILY)
  1142. return link->l_family;
  1143. else
  1144. return AF_UNSPEC;
  1145. }
  1146. void rtnl_link_set_arptype(struct rtnl_link *link, unsigned int arptype)
  1147. {
  1148. link->l_arptype = arptype;
  1149. }
  1150. unsigned int rtnl_link_get_arptype(struct rtnl_link *link)
  1151. {
  1152. return link->l_arptype;
  1153. }
  1154. void rtnl_link_set_ifindex(struct rtnl_link *link, int ifindex)
  1155. {
  1156. link->l_index = ifindex;
  1157. link->ce_mask |= LINK_ATTR_IFINDEX;
  1158. }
  1159. int rtnl_link_get_ifindex(struct rtnl_link *link)
  1160. {
  1161. if (link->ce_mask & LINK_ATTR_IFINDEX)
  1162. return link->l_index;
  1163. else
  1164. return RTNL_LINK_NOT_FOUND;
  1165. }
  1166. void rtnl_link_set_mtu(struct rtnl_link *link, unsigned int mtu)
  1167. {
  1168. link->l_mtu = mtu;
  1169. link->ce_mask |= LINK_ATTR_MTU;
  1170. }
  1171. unsigned int rtnl_link_get_mtu(struct rtnl_link *link)
  1172. {
  1173. if (link->ce_mask & LINK_ATTR_MTU)
  1174. return link->l_mtu;
  1175. else
  1176. return 0;
  1177. }
  1178. void rtnl_link_set_txqlen(struct rtnl_link *link, unsigned int txqlen)
  1179. {
  1180. link->l_txqlen = txqlen;
  1181. link->ce_mask |= LINK_ATTR_TXQLEN;
  1182. }
  1183. unsigned int rtnl_link_get_txqlen(struct rtnl_link *link)
  1184. {
  1185. if (link->ce_mask & LINK_ATTR_TXQLEN)
  1186. return link->l_txqlen;
  1187. else
  1188. return UINT_MAX;
  1189. }
  1190. void rtnl_link_set_weight(struct rtnl_link *link, unsigned int weight)
  1191. {
  1192. link->l_weight = weight;
  1193. link->ce_mask |= LINK_ATTR_WEIGHT;
  1194. }
  1195. unsigned int rtnl_link_get_weight(struct rtnl_link *link)
  1196. {
  1197. if (link->ce_mask & LINK_ATTR_WEIGHT)
  1198. return link->l_weight;
  1199. else
  1200. return UINT_MAX;
  1201. }
  1202. void rtnl_link_set_link(struct rtnl_link *link, int ifindex)
  1203. {
  1204. link->l_link = ifindex;
  1205. link->ce_mask |= LINK_ATTR_LINK;
  1206. }
  1207. int rtnl_link_get_link(struct rtnl_link *link)
  1208. {
  1209. if (link->ce_mask & LINK_ATTR_LINK)
  1210. return link->l_link;
  1211. else
  1212. return RTNL_LINK_NOT_FOUND;
  1213. }
  1214. void rtnl_link_set_master(struct rtnl_link *link, int ifindex)
  1215. {
  1216. link->l_master = ifindex;
  1217. link->ce_mask |= LINK_ATTR_MASTER;
  1218. }
  1219. int rtnl_link_get_master(struct rtnl_link *link)
  1220. {
  1221. if (link->ce_mask & LINK_ATTR_MASTER)
  1222. return link->l_master;
  1223. else
  1224. return RTNL_LINK_NOT_FOUND;
  1225. }
  1226. void rtnl_link_set_operstate(struct rtnl_link *link, uint8_t operstate)
  1227. {
  1228. link->l_operstate = operstate;
  1229. link->ce_mask |= LINK_ATTR_OPERSTATE;
  1230. }
  1231. uint8_t rtnl_link_get_operstate(struct rtnl_link *link)
  1232. {
  1233. if (link->ce_mask & LINK_ATTR_OPERSTATE)
  1234. return link->l_operstate;
  1235. else
  1236. return IF_OPER_UNKNOWN;
  1237. }
  1238. void rtnl_link_set_linkmode(struct rtnl_link *link, uint8_t linkmode)
  1239. {
  1240. link->l_linkmode = linkmode;
  1241. link->ce_mask |= LINK_ATTR_LINKMODE;
  1242. }
  1243. uint8_t rtnl_link_get_linkmode(struct rtnl_link *link)
  1244. {
  1245. if (link->ce_mask & LINK_ATTR_LINKMODE)
  1246. return link->l_linkmode;
  1247. else
  1248. return IF_LINK_MODE_DEFAULT;
  1249. }
  1250. uint64_t rtnl_link_get_stat(struct rtnl_link *link, int id)
  1251. {
  1252. if (id < 0 || id > RTNL_LINK_STATS_MAX)
  1253. return 0;
  1254. return link->l_stats[id];
  1255. }
  1256. /**
  1257. * Specify the info type of a link
  1258. * @arg link link object
  1259. * @arg type info type
  1260. *
  1261. * Looks up the info type and prepares the link to store info type
  1262. * specific attributes. If an info type has been assigned already
  1263. * it will be released with all changes lost.
  1264. *
  1265. * @return 0 on success or a negative errror code.
  1266. */
  1267. int rtnl_link_set_info_type(struct rtnl_link *link, const char *type)
  1268. {
  1269. struct rtnl_link_info_ops *io;
  1270. int err;
  1271. if ((io = rtnl_link_info_ops_lookup(type)) == NULL)
  1272. return nl_error(ENOENT, "No such link info type exists");
  1273. if (link->l_info_ops)
  1274. release_link_info(link);
  1275. if ((err = io->io_alloc(link)) < 0)
  1276. return err;
  1277. link->l_info_ops = io;
  1278. return 0;
  1279. }
  1280. /**
  1281. * Return info type of a link
  1282. * @arg link link object
  1283. *
  1284. * @note The returned pointer is only valid as long as the link exists
  1285. * @return Info type name or NULL if unknown.
  1286. */
  1287. char *rtnl_link_get_info_type(struct rtnl_link *link)
  1288. {
  1289. if (link->l_info_ops)
  1290. return link->l_info_ops->io_name;
  1291. else
  1292. return NULL;
  1293. }
  1294. /** @} */
  1295. static struct nl_object_ops link_obj_ops = {
  1296. .oo_name = "route/link",
  1297. .oo_size = sizeof(struct rtnl_link),
  1298. .oo_free_data = link_free_data,
  1299. .oo_clone = link_clone,
  1300. .oo_dump[NL_DUMP_BRIEF] = link_dump_brief,
  1301. .oo_dump[NL_DUMP_FULL] = link_dump_full,
  1302. .oo_dump[NL_DUMP_STATS] = link_dump_stats,
  1303. .oo_dump[NL_DUMP_XML] = link_dump_xml,
  1304. .oo_dump[NL_DUMP_ENV] = link_dump_env,
  1305. .oo_compare = link_compare,
  1306. .oo_attrs2str = link_attrs2str,
  1307. .oo_id_attrs = LINK_ATTR_IFINDEX,
  1308. };
  1309. static struct nl_af_group link_groups[] = {
  1310. { AF_UNSPEC, RTNLGRP_LINK },
  1311. { END_OF_GROUP_LIST },
  1312. };
  1313. static struct nl_cache_ops rtnl_link_ops = {
  1314. .co_name = "route/link",
  1315. .co_hdrsize = sizeof(struct ifinfomsg),
  1316. .co_msgtypes = {
  1317. { RTM_NEWLINK, NL_ACT_NEW, "new" },
  1318. { RTM_DELLINK, NL_ACT_DEL, "del" },
  1319. { RTM_GETLINK, NL_ACT_GET, "get" },
  1320. END_OF_MSGTYPES_LIST,
  1321. },
  1322. .co_protocol = NETLINK_ROUTE,
  1323. .co_groups = link_groups,
  1324. .co_request_update = link_request_update,
  1325. .co_msg_parser = link_msg_parser,
  1326. .co_obj_ops = &link_obj_ops,
  1327. };
  1328. static void __init link_init(void)
  1329. {
  1330. nl_cache_mngt_register(&rtnl_link_ops);
  1331. }
  1332. static void __exit link_exit(void)
  1333. {
  1334. nl_cache_mngt_unregister(&rtnl_link_ops);
  1335. }
  1336. /** @} */