vxlan.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * lib/route/link/vxlan.c VXLAN Link Info
  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) 2013 Yasunobu Chiba <yasu@dsl.gr.jp>
  10. */
  11. /**
  12. * @ingroup link
  13. * @defgroup vxlan VXLAN
  14. * Virtual eXtensible Local Area Network link module
  15. *
  16. * @details
  17. * \b Link Type Name: "vxlan"
  18. *
  19. * @route_doc{link_vxlan, VXLAN Documentation}
  20. *
  21. * @{
  22. */
  23. #include <netlink-private/netlink.h>
  24. #include <netlink/netlink.h>
  25. #include <netlink/attr.h>
  26. #include <netlink/utils.h>
  27. #include <netlink/object.h>
  28. #include <netlink/route/rtnl.h>
  29. #include <netlink-private/route/link/api.h>
  30. #include <netlink/route/link/vxlan.h>
  31. #include <linux/if_link.h>
  32. /** @cond SKIP */
  33. #define VXLAN_HAS_ID (1<<0)
  34. #define VXLAN_HAS_GROUP (1<<1)
  35. #define VXLAN_HAS_LINK (1<<2)
  36. #define VXLAN_HAS_LOCAL (1<<3)
  37. #define VXLAN_HAS_TTL (1<<4)
  38. #define VXLAN_HAS_TOS (1<<5)
  39. #define VXLAN_HAS_LEARNING (1<<6)
  40. #define VXLAN_HAS_AGEING (1<<7)
  41. #define VXLAN_HAS_LIMIT (1<<8)
  42. #define VXLAN_HAS_PORT_RANGE (1<<9)
  43. #define VXLAN_HAS_PROXY (1<<10)
  44. #define VXLAN_HAS_RSC (1<<11)
  45. #define VXLAN_HAS_L2MISS (1<<12)
  46. #define VXLAN_HAS_L3MISS (1<<13)
  47. struct vxlan_info
  48. {
  49. uint32_t vxi_id;
  50. uint32_t vxi_group;
  51. uint32_t vxi_link;
  52. uint32_t vxi_local;
  53. uint8_t vxi_ttl;
  54. uint8_t vxi_tos;
  55. uint8_t vxi_learning;
  56. uint32_t vxi_ageing;
  57. uint32_t vxi_limit;
  58. struct ifla_vxlan_port_range vxi_port_range;
  59. uint8_t vxi_proxy;
  60. uint8_t vxi_rsc;
  61. uint8_t vxi_l2miss;
  62. uint8_t vxi_l3miss;
  63. uint32_t vxi_mask;
  64. };
  65. /** @endcond */
  66. static struct nla_policy vxlan_policy[IFLA_VXLAN_MAX+1] = {
  67. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  68. [IFLA_VXLAN_GROUP] = { .minlen = sizeof(uint32_t) },
  69. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  70. [IFLA_VXLAN_LOCAL] = { .minlen = sizeof(uint32_t) },
  71. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  72. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  73. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  74. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  75. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  76. [IFLA_VXLAN_PORT_RANGE] = { .minlen = sizeof(struct ifla_vxlan_port_range) },
  77. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  78. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  79. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  80. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  81. };
  82. static int vxlan_alloc(struct rtnl_link *link)
  83. {
  84. struct vxlan_info *vxi;
  85. if ((vxi = calloc(1, sizeof(*vxi))) == NULL)
  86. return -NLE_NOMEM;
  87. link->l_info = vxi;
  88. return 0;
  89. }
  90. static int vxlan_parse(struct rtnl_link *link, struct nlattr *data,
  91. struct nlattr *xstats)
  92. {
  93. struct nlattr *tb[IFLA_VXLAN_MAX+1];
  94. struct vxlan_info *vxi;
  95. int err;
  96. NL_DBG(3, "Parsing VXLAN link info");
  97. if ((err = nla_parse_nested(tb, IFLA_VXLAN_MAX, data, vxlan_policy)) < 0)
  98. goto errout;
  99. if ((err = vxlan_alloc(link)) < 0)
  100. goto errout;
  101. vxi = link->l_info;
  102. if (tb[IFLA_VXLAN_ID]) {
  103. vxi->vxi_id = nla_get_u32(tb[IFLA_VXLAN_ID]);
  104. vxi->vxi_mask |= VXLAN_HAS_ID;
  105. }
  106. if (tb[IFLA_VXLAN_GROUP]) {
  107. nla_memcpy(&vxi->vxi_group, tb[IFLA_VXLAN_GROUP],
  108. sizeof(vxi->vxi_group));
  109. vxi->vxi_mask |= VXLAN_HAS_GROUP;
  110. }
  111. if (tb[IFLA_VXLAN_LINK]) {
  112. vxi->vxi_link = nla_get_u32(tb[IFLA_VXLAN_LINK]);
  113. vxi->vxi_mask |= VXLAN_HAS_LINK;
  114. }
  115. if (tb[IFLA_VXLAN_LOCAL]) {
  116. nla_memcpy(&vxi->vxi_local, tb[IFLA_VXLAN_LOCAL],
  117. sizeof(vxi->vxi_local));
  118. vxi->vxi_mask |= VXLAN_HAS_LOCAL;
  119. }
  120. if (tb[IFLA_VXLAN_TTL]) {
  121. vxi->vxi_ttl = nla_get_u8(tb[IFLA_VXLAN_TTL]);
  122. vxi->vxi_mask |= VXLAN_HAS_TTL;
  123. }
  124. if (tb[IFLA_VXLAN_TOS]) {
  125. vxi->vxi_tos = nla_get_u8(tb[IFLA_VXLAN_TOS]);
  126. vxi->vxi_mask |= VXLAN_HAS_TOS;
  127. }
  128. if (tb[IFLA_VXLAN_LEARNING]) {
  129. vxi->vxi_learning = nla_get_u8(tb[IFLA_VXLAN_LEARNING]);
  130. vxi->vxi_mask |= VXLAN_HAS_LEARNING;
  131. }
  132. if (tb[IFLA_VXLAN_AGEING]) {
  133. vxi->vxi_ageing = nla_get_u32(tb[IFLA_VXLAN_AGEING]);
  134. vxi->vxi_mask |= VXLAN_HAS_AGEING;
  135. }
  136. if (tb[IFLA_VXLAN_LIMIT]) {
  137. vxi->vxi_limit = nla_get_u32(tb[IFLA_VXLAN_LIMIT]);
  138. vxi->vxi_mask |= VXLAN_HAS_LIMIT;
  139. }
  140. if (tb[IFLA_VXLAN_PORT_RANGE]) {
  141. nla_memcpy(&vxi->vxi_port_range, tb[IFLA_VXLAN_PORT_RANGE],
  142. sizeof(vxi->vxi_port_range));
  143. vxi->vxi_mask |= VXLAN_HAS_PORT_RANGE;
  144. }
  145. if (tb[IFLA_VXLAN_PROXY]) {
  146. vxi->vxi_proxy = nla_get_u8(tb[IFLA_VXLAN_PROXY]);
  147. vxi->vxi_mask |= VXLAN_HAS_PROXY;
  148. }
  149. if (tb[IFLA_VXLAN_RSC]) {
  150. vxi->vxi_rsc = nla_get_u8(tb[IFLA_VXLAN_RSC]);
  151. vxi->vxi_mask |= VXLAN_HAS_RSC;
  152. }
  153. if (tb[IFLA_VXLAN_L2MISS]) {
  154. vxi->vxi_l2miss = nla_get_u8(tb[IFLA_VXLAN_L2MISS]);
  155. vxi->vxi_mask |= VXLAN_HAS_L2MISS;
  156. }
  157. if (tb[IFLA_VXLAN_L3MISS]) {
  158. vxi->vxi_l3miss = nla_get_u8(tb[IFLA_VXLAN_L3MISS]);
  159. vxi->vxi_mask |= VXLAN_HAS_L3MISS;
  160. }
  161. err = 0;
  162. errout:
  163. return err;
  164. }
  165. static void vxlan_free(struct rtnl_link *link)
  166. {
  167. struct vxlan_info *vxi = link->l_info;
  168. free(vxi);
  169. link->l_info = NULL;
  170. }
  171. static void vxlan_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
  172. {
  173. struct vxlan_info *vxi = link->l_info;
  174. nl_dump(p, "vxlan-id %u", vxi->vxi_id);
  175. }
  176. static void vxlan_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
  177. {
  178. struct vxlan_info *vxi = link->l_info;
  179. char *name, addr[INET_ADDRSTRLEN];
  180. nl_dump_line(p, " vxlan-id %u\n", vxi->vxi_id);
  181. if (vxi->vxi_mask & VXLAN_HAS_GROUP) {
  182. nl_dump(p, " group ");
  183. if(inet_ntop(AF_INET, &vxi->vxi_group, addr, sizeof(addr)))
  184. nl_dump_line(p, "%s\n", addr);
  185. else
  186. nl_dump_line(p, "%#x\n", ntohs(vxi->vxi_group));
  187. }
  188. if (vxi->vxi_mask & VXLAN_HAS_LINK) {
  189. nl_dump(p, " link ");
  190. name = rtnl_link_get_name(link);
  191. if (name)
  192. nl_dump_line(p, "%s\n", name);
  193. else
  194. nl_dump_line(p, "%u\n", vxi->vxi_link);
  195. }
  196. if (vxi->vxi_mask & VXLAN_HAS_LOCAL) {
  197. nl_dump(p, " local ");
  198. if(inet_ntop(AF_INET, &vxi->vxi_local, addr, sizeof(addr)))
  199. nl_dump_line(p, "%s\n", addr);
  200. else
  201. nl_dump_line(p, "%#x\n", ntohs(vxi->vxi_local));
  202. }
  203. if (vxi->vxi_mask & VXLAN_HAS_TTL) {
  204. nl_dump(p, " ttl ");
  205. if(vxi->vxi_ttl)
  206. nl_dump_line(p, "%u\n", vxi->vxi_ttl);
  207. else
  208. nl_dump_line(p, "inherit\n");
  209. }
  210. if (vxi->vxi_mask & VXLAN_HAS_TOS) {
  211. nl_dump(p, " tos ");
  212. if (vxi->vxi_tos == 1)
  213. nl_dump_line(p, "inherit\n", vxi->vxi_tos);
  214. else
  215. nl_dump_line(p, "%#x\n", vxi->vxi_tos);
  216. }
  217. if (vxi->vxi_mask & VXLAN_HAS_LEARNING) {
  218. nl_dump(p, " learning ");
  219. if (vxi->vxi_learning)
  220. nl_dump_line(p, "enabled (%#x)\n", vxi->vxi_learning);
  221. else
  222. nl_dump_line(p, "disabled\n");
  223. }
  224. if (vxi->vxi_mask & VXLAN_HAS_AGEING) {
  225. nl_dump(p, " ageing ");
  226. if (vxi->vxi_ageing)
  227. nl_dump_line(p, "%u seconds\n", vxi->vxi_ageing);
  228. else
  229. nl_dump_line(p, "disabled\n");
  230. }
  231. if (vxi->vxi_mask & VXLAN_HAS_LIMIT) {
  232. nl_dump(p, " limit ");
  233. if (vxi->vxi_limit)
  234. nl_dump_line(p, "%u\n", vxi->vxi_limit);
  235. else
  236. nl_dump_line(p, "unlimited\n");
  237. }
  238. if (vxi->vxi_mask & VXLAN_HAS_PORT_RANGE)
  239. nl_dump_line(p, " port range %u - %u\n",
  240. ntohs(vxi->vxi_port_range.low),
  241. ntohs(vxi->vxi_port_range.high));
  242. if (vxi->vxi_mask & VXLAN_HAS_PROXY) {
  243. nl_dump(p, " proxy ");
  244. if (vxi->vxi_proxy)
  245. nl_dump_line(p, "enabled (%#x)\n", vxi->vxi_proxy);
  246. else
  247. nl_dump_line(p, "disabled\n");
  248. }
  249. if (vxi->vxi_mask & VXLAN_HAS_RSC) {
  250. nl_dump(p, " rsc ");
  251. if (vxi->vxi_rsc)
  252. nl_dump_line(p, "enabled (%#x)\n", vxi->vxi_rsc);
  253. else
  254. nl_dump_line(p, "disabled\n");
  255. }
  256. if (vxi->vxi_mask & VXLAN_HAS_L2MISS) {
  257. nl_dump(p, " l2miss ");
  258. if (vxi->vxi_l2miss)
  259. nl_dump_line(p, "enabled (%#x)\n", vxi->vxi_l2miss);
  260. else
  261. nl_dump_line(p, "disabled\n");
  262. }
  263. if (vxi->vxi_mask & VXLAN_HAS_L3MISS) {
  264. nl_dump(p, " l3miss ");
  265. if (vxi->vxi_l3miss)
  266. nl_dump_line(p, "enabled (%#x)\n", vxi->vxi_l3miss);
  267. else
  268. nl_dump_line(p, "disabled\n");
  269. }
  270. }
  271. static int vxlan_clone(struct rtnl_link *dst, struct rtnl_link *src)
  272. {
  273. struct vxlan_info *vdst, *vsrc = src->l_info;
  274. int err;
  275. dst->l_info = NULL;
  276. if ((err = rtnl_link_set_type(dst, "vxlan")) < 0)
  277. return err;
  278. vdst = dst->l_info;
  279. if (!vdst || !vsrc)
  280. return -NLE_NOMEM;
  281. memcpy(vdst, vsrc, sizeof(struct vxlan_info));
  282. return 0;
  283. }
  284. static int vxlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
  285. {
  286. struct vxlan_info *vxi = link->l_info;
  287. struct nlattr *data;
  288. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  289. return -NLE_MSGSIZE;
  290. if (vxi->vxi_mask & VXLAN_HAS_ID)
  291. NLA_PUT_U32(msg, IFLA_VXLAN_ID, vxi->vxi_id);
  292. if (vxi->vxi_mask & VXLAN_HAS_GROUP)
  293. NLA_PUT(msg, IFLA_VXLAN_GROUP, sizeof(vxi->vxi_group), &vxi->vxi_group);
  294. if (vxi->vxi_mask & VXLAN_HAS_LINK)
  295. NLA_PUT_U32(msg, IFLA_VXLAN_LINK, vxi->vxi_link);
  296. if (vxi->vxi_mask & VXLAN_HAS_LOCAL)
  297. NLA_PUT(msg, IFLA_VXLAN_LOCAL, sizeof(vxi->vxi_local), &vxi->vxi_local);
  298. if (vxi->vxi_mask & VXLAN_HAS_TTL)
  299. NLA_PUT_U8(msg, IFLA_VXLAN_TTL, vxi->vxi_ttl);
  300. if (vxi->vxi_mask & VXLAN_HAS_TOS)
  301. NLA_PUT_U8(msg, IFLA_VXLAN_TOS, vxi->vxi_tos);
  302. if (vxi->vxi_mask & VXLAN_HAS_LEARNING)
  303. NLA_PUT_U8(msg, IFLA_VXLAN_LEARNING, vxi->vxi_learning);
  304. if (vxi->vxi_mask & VXLAN_HAS_AGEING)
  305. NLA_PUT_U32(msg, IFLA_VXLAN_AGEING, vxi->vxi_ageing);
  306. if (vxi->vxi_mask & VXLAN_HAS_LIMIT)
  307. NLA_PUT_U32(msg, IFLA_VXLAN_LIMIT, vxi->vxi_limit);
  308. if (vxi->vxi_mask & VXLAN_HAS_PORT_RANGE)
  309. NLA_PUT(msg, IFLA_VXLAN_PORT_RANGE, sizeof(vxi->vxi_port_range),
  310. &vxi->vxi_port_range);
  311. if (vxi->vxi_mask & VXLAN_HAS_PROXY)
  312. NLA_PUT_U8(msg, IFLA_VXLAN_PROXY, vxi->vxi_proxy);
  313. if (vxi->vxi_mask & VXLAN_HAS_RSC)
  314. NLA_PUT_U8(msg, IFLA_VXLAN_RSC, vxi->vxi_rsc);
  315. if (vxi->vxi_mask & VXLAN_HAS_L2MISS)
  316. NLA_PUT_U8(msg, IFLA_VXLAN_L2MISS, vxi->vxi_l2miss);
  317. if (vxi->vxi_mask & VXLAN_HAS_L3MISS)
  318. NLA_PUT_U8(msg, IFLA_VXLAN_L3MISS, vxi->vxi_l3miss);
  319. nla_nest_end(msg, data);
  320. nla_put_failure:
  321. return 0;
  322. }
  323. static struct rtnl_link_info_ops vxlan_info_ops = {
  324. .io_name = "vxlan",
  325. .io_alloc = vxlan_alloc,
  326. .io_parse = vxlan_parse,
  327. .io_dump = {
  328. [NL_DUMP_LINE] = vxlan_dump_line,
  329. [NL_DUMP_DETAILS] = vxlan_dump_details,
  330. },
  331. .io_clone = vxlan_clone,
  332. .io_put_attrs = vxlan_put_attrs,
  333. .io_free = vxlan_free,
  334. };
  335. /** @cond SKIP */
  336. #define IS_VXLAN_LINK_ASSERT(link) \
  337. if ((link)->l_info_ops != &vxlan_info_ops) { \
  338. APPBUG("Link is not a vxlan link. set type \"vxlan\" first."); \
  339. return -NLE_OPNOTSUPP; \
  340. }
  341. /** @endcond */
  342. /**
  343. * @name VXLAN Object
  344. * @{
  345. */
  346. /**
  347. * Allocate link object of type VXLAN
  348. *
  349. * @return Allocated link object or NULL.
  350. */
  351. struct rtnl_link *rtnl_link_vxlan_alloc(void)
  352. {
  353. struct rtnl_link *link;
  354. int err;
  355. if (!(link = rtnl_link_alloc()))
  356. return NULL;
  357. if ((err = rtnl_link_set_type(link, "vxlan")) < 0) {
  358. rtnl_link_put(link);
  359. return NULL;
  360. }
  361. return link;
  362. }
  363. /**
  364. * Check if link is a VXLAN link
  365. * @arg link Link object
  366. *
  367. * @return True if link is a VXLAN link, otherwise false is returned.
  368. */
  369. int rtnl_link_is_vxlan(struct rtnl_link *link)
  370. {
  371. return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vxlan");
  372. }
  373. /**
  374. * Set VXLAN Network Identifier
  375. * @arg link Link object
  376. * @arg id VXLAN network identifier (or VXLAN segment identifier)
  377. *
  378. * @return 0 on success or a negative error code
  379. */
  380. int rtnl_link_vxlan_set_id(struct rtnl_link *link, uint32_t id)
  381. {
  382. struct vxlan_info *vxi = link->l_info;
  383. IS_VXLAN_LINK_ASSERT(link);
  384. if (id > VXLAN_ID_MAX)
  385. return -NLE_INVAL;
  386. vxi->vxi_id = id;
  387. vxi->vxi_mask |= VXLAN_HAS_ID;
  388. return 0;
  389. }
  390. /**
  391. * Get VXLAN Network Identifier
  392. * @arg link Link object
  393. * @arg id Pointer to store network identifier
  394. *
  395. * @return 0 on success or a negative error code
  396. */
  397. int rtnl_link_vxlan_get_id(struct rtnl_link *link, uint32_t *id)
  398. {
  399. struct vxlan_info *vxi = link->l_info;
  400. IS_VXLAN_LINK_ASSERT(link);
  401. if(!id)
  402. return -NLE_INVAL;
  403. if (vxi->vxi_mask & VXLAN_HAS_ID)
  404. *id = vxi->vxi_id;
  405. else
  406. return -NLE_AGAIN;
  407. return 0;
  408. }
  409. /**
  410. * Set VXLAN multicast IP address
  411. * @arg link Link object
  412. * @arg addr Multicast IP address to join
  413. *
  414. * @return 0 on success or a negative error code
  415. */
  416. int rtnl_link_vxlan_set_group(struct rtnl_link *link, struct nl_addr *addr)
  417. {
  418. struct vxlan_info *vxi = link->l_info;
  419. IS_VXLAN_LINK_ASSERT(link);
  420. if ((nl_addr_get_family(addr) != AF_INET) ||
  421. (nl_addr_get_len(addr) != sizeof(vxi->vxi_group)))
  422. return -NLE_INVAL;
  423. memcpy(&vxi->vxi_group, nl_addr_get_binary_addr(addr),
  424. sizeof(vxi->vxi_group));
  425. vxi->vxi_mask |= VXLAN_HAS_GROUP;
  426. return 0;
  427. }
  428. /**
  429. * Get VXLAN multicast IP address
  430. * @arg link Link object
  431. * @arg addr Pointer to store multicast IP address
  432. *
  433. * @return 0 on success or a negative error code
  434. */
  435. int rtnl_link_vxlan_get_group(struct rtnl_link *link, struct nl_addr **addr)
  436. {
  437. struct vxlan_info *vxi = link->l_info;
  438. IS_VXLAN_LINK_ASSERT(link);
  439. if (!addr)
  440. return -NLE_INVAL;
  441. if (!(vxi->vxi_mask & VXLAN_HAS_GROUP))
  442. return -NLE_AGAIN;
  443. *addr = nl_addr_build(AF_INET, &vxi->vxi_group, sizeof(vxi->vxi_group));
  444. return 0;
  445. }
  446. /**
  447. * Set physical device to use for VXLAN
  448. * @arg link Link object
  449. * @arg index Interface index
  450. *
  451. * @return 0 on success or a negative error code
  452. */
  453. int rtnl_link_vxlan_set_link(struct rtnl_link *link, uint32_t index)
  454. {
  455. struct vxlan_info *vxi = link->l_info;
  456. IS_VXLAN_LINK_ASSERT(link);
  457. vxi->vxi_link = index;
  458. vxi->vxi_mask |= VXLAN_HAS_LINK;
  459. return 0;
  460. }
  461. /**
  462. * Get physical device to use for VXLAN
  463. * @arg link Link object
  464. * @arg index Pointer to store interface index
  465. *
  466. * @return 0 on success or a negative error code
  467. */
  468. int rtnl_link_vxlan_get_link(struct rtnl_link *link, uint32_t *index)
  469. {
  470. struct vxlan_info *vxi = link->l_info;
  471. IS_VXLAN_LINK_ASSERT(link);
  472. if (!index)
  473. return -NLE_INVAL;
  474. if (!(vxi->vxi_mask & VXLAN_HAS_LINK))
  475. return -NLE_AGAIN;
  476. *index = vxi->vxi_link;
  477. return 0;
  478. }
  479. /**
  480. * Set source address to use for VXLAN
  481. * @arg link Link object
  482. * @arg addr Local address
  483. *
  484. * @return 0 on success or a negative error code
  485. */
  486. int rtnl_link_vxlan_set_local(struct rtnl_link *link, struct nl_addr *addr)
  487. {
  488. struct vxlan_info *vxi = link->l_info;
  489. IS_VXLAN_LINK_ASSERT(link);
  490. if ((nl_addr_get_family(addr) != AF_INET) ||
  491. (nl_addr_get_len(addr) != sizeof(vxi->vxi_local)))
  492. return -NLE_INVAL;
  493. memcpy(&vxi->vxi_local, nl_addr_get_binary_addr(addr),
  494. sizeof(vxi->vxi_local));
  495. vxi->vxi_mask |= VXLAN_HAS_LOCAL;
  496. return 0;
  497. }
  498. /**
  499. * Get source address to use for VXLAN
  500. * @arg link Link object
  501. * @arg addr Pointer to store local address
  502. *
  503. * @return 0 on success or a negative error code
  504. */
  505. int rtnl_link_vxlan_get_local(struct rtnl_link *link, struct nl_addr **addr)
  506. {
  507. struct vxlan_info *vxi = link->l_info;
  508. IS_VXLAN_LINK_ASSERT(link);
  509. if (!addr)
  510. return -NLE_INVAL;
  511. if (!(vxi->vxi_mask & VXLAN_HAS_LOCAL))
  512. return -NLE_AGAIN;
  513. *addr = nl_addr_build(AF_INET, &vxi->vxi_local, sizeof(vxi->vxi_local));
  514. return 0;
  515. }
  516. /**
  517. * Set IP TTL value to use for VXLAN
  518. * @arg link Link object
  519. * @arg ttl TTL value
  520. *
  521. * @return 0 on success or a negative error code
  522. */
  523. int rtnl_link_vxlan_set_ttl(struct rtnl_link *link, uint8_t ttl)
  524. {
  525. struct vxlan_info *vxi = link->l_info;
  526. IS_VXLAN_LINK_ASSERT(link);
  527. vxi->vxi_ttl = ttl;
  528. vxi->vxi_mask |= VXLAN_HAS_TTL;
  529. return 0;
  530. }
  531. /**
  532. * Get IP TTL value to use for VXLAN
  533. * @arg link Link object
  534. *
  535. * @return TTL value on success or a negative error code
  536. */
  537. int rtnl_link_vxlan_get_ttl(struct rtnl_link *link)
  538. {
  539. struct vxlan_info *vxi = link->l_info;
  540. IS_VXLAN_LINK_ASSERT(link);
  541. if (!(vxi->vxi_mask & VXLAN_HAS_TTL))
  542. return -NLE_AGAIN;
  543. return vxi->vxi_ttl;
  544. }
  545. /**
  546. * Set IP ToS value to use for VXLAN
  547. * @arg link Link object
  548. * @arg tos ToS value
  549. *
  550. * @return 0 on success or a negative error code
  551. */
  552. int rtnl_link_vxlan_set_tos(struct rtnl_link *link, uint8_t tos)
  553. {
  554. struct vxlan_info *vxi = link->l_info;
  555. IS_VXLAN_LINK_ASSERT(link);
  556. vxi->vxi_tos = tos;
  557. vxi->vxi_mask |= VXLAN_HAS_TOS;
  558. return 0;
  559. }
  560. /**
  561. * Get IP ToS value to use for VXLAN
  562. * @arg link Link object
  563. *
  564. * @return ToS value on success or a negative error code
  565. */
  566. int rtnl_link_vxlan_get_tos(struct rtnl_link *link)
  567. {
  568. struct vxlan_info *vxi = link->l_info;
  569. IS_VXLAN_LINK_ASSERT(link);
  570. if (!(vxi->vxi_mask & VXLAN_HAS_TOS))
  571. return -NLE_AGAIN;
  572. return vxi->vxi_tos;
  573. }
  574. /**
  575. * Set VXLAN learning status
  576. * @arg link Link object
  577. * @arg learning Learning status value
  578. *
  579. * @return 0 on success or a negative error code
  580. */
  581. int rtnl_link_vxlan_set_learning(struct rtnl_link *link, uint8_t learning)
  582. {
  583. struct vxlan_info *vxi = link->l_info;
  584. IS_VXLAN_LINK_ASSERT(link);
  585. vxi->vxi_learning = learning;
  586. vxi->vxi_mask |= VXLAN_HAS_LEARNING;
  587. return 0;
  588. }
  589. /**
  590. * Get VXLAN learning status
  591. * @arg link Link object
  592. *
  593. * @return Learning status value on success or a negative error code
  594. */
  595. int rtnl_link_vxlan_get_learning(struct rtnl_link *link)
  596. {
  597. struct vxlan_info *vxi = link->l_info;
  598. IS_VXLAN_LINK_ASSERT(link);
  599. if (!(vxi->vxi_mask & VXLAN_HAS_LEARNING))
  600. return -NLE_AGAIN;
  601. return vxi->vxi_learning;
  602. }
  603. /**
  604. * Enable VXLAN address learning
  605. * @arg link Link object
  606. *
  607. * @return 0 on success or a negative error code
  608. */
  609. int rtnl_link_vxlan_enable_learning(struct rtnl_link *link)
  610. {
  611. return rtnl_link_vxlan_set_learning(link, 1);
  612. }
  613. /**
  614. * Disable VXLAN address learning
  615. * @arg link Link object
  616. *
  617. * @return 0 on success or a negative error code
  618. */
  619. int rtnl_link_vxlan_disable_learning(struct rtnl_link *link)
  620. {
  621. return rtnl_link_vxlan_set_learning(link, 0);
  622. }
  623. /**
  624. * Set expiration timer value to use for VXLAN
  625. * @arg link Link object
  626. * @arg expiry Expiration timer value
  627. *
  628. * @return 0 on success or a negative error code
  629. */
  630. int rtnl_link_vxlan_set_ageing(struct rtnl_link *link, uint32_t expiry)
  631. {
  632. struct vxlan_info *vxi = link->l_info;
  633. IS_VXLAN_LINK_ASSERT(link);
  634. vxi->vxi_ageing = expiry;
  635. vxi->vxi_mask |= VXLAN_HAS_AGEING;
  636. return 0;
  637. }
  638. /**
  639. * Get expiration timer value to use for VXLAN
  640. * @arg link Link object
  641. * @arg expiry Pointer to store expiration timer value
  642. *
  643. * @return 0 on success or a negative error code
  644. */
  645. int rtnl_link_vxlan_get_ageing(struct rtnl_link *link, uint32_t *expiry)
  646. {
  647. struct vxlan_info *vxi = link->l_info;
  648. IS_VXLAN_LINK_ASSERT(link);
  649. if (!expiry)
  650. return -NLE_INVAL;
  651. if (vxi->vxi_mask & VXLAN_HAS_AGEING)
  652. *expiry = vxi->vxi_ageing;
  653. else
  654. return -NLE_AGAIN;
  655. return 0;
  656. }
  657. /**
  658. * Set maximum number of forwarding database entries to use for VXLAN
  659. * @arg link Link object
  660. * @arg limit Maximum number
  661. *
  662. * @return 0 on success or a negative error code
  663. */
  664. int rtnl_link_vxlan_set_limit(struct rtnl_link *link, uint32_t limit)
  665. {
  666. struct vxlan_info *vxi = link->l_info;
  667. IS_VXLAN_LINK_ASSERT(link);
  668. vxi->vxi_limit = limit;
  669. vxi->vxi_mask |= VXLAN_HAS_LIMIT;
  670. return 0;
  671. }
  672. /**
  673. * Get maximum number of forwarding database entries to use for VXLAN
  674. * @arg link Link object
  675. * @arg limit Pointer to store maximum number
  676. *
  677. * @return 0 on success or a negative error code
  678. */
  679. int rtnl_link_vxlan_get_limit(struct rtnl_link *link, uint32_t *limit)
  680. {
  681. struct vxlan_info *vxi = link->l_info;
  682. IS_VXLAN_LINK_ASSERT(link);
  683. if (!limit)
  684. return -NLE_INVAL;
  685. if (vxi->vxi_mask & VXLAN_HAS_LIMIT)
  686. *limit = vxi->vxi_limit;
  687. else
  688. return -NLE_AGAIN;
  689. return 0;
  690. }
  691. /**
  692. * Set range of UDP port numbers to use for VXLAN
  693. * @arg link Link object
  694. * @arg range Port number range
  695. *
  696. * @return 0 on success or a negative error code
  697. */
  698. int rtnl_link_vxlan_set_port_range(struct rtnl_link *link,
  699. struct ifla_vxlan_port_range *range)
  700. {
  701. struct vxlan_info *vxi = link->l_info;
  702. IS_VXLAN_LINK_ASSERT(link);
  703. if (!range)
  704. return -NLE_INVAL;
  705. memcpy(&vxi->vxi_port_range, range, sizeof(vxi->vxi_port_range));
  706. vxi->vxi_mask |= VXLAN_HAS_PORT_RANGE;
  707. return 0;
  708. }
  709. /**
  710. * Get range of UDP port numbers to use for VXLAN
  711. * @arg link Link object
  712. * @arg range Pointer to store port range
  713. *
  714. * @return 0 on success or a negative error code
  715. */
  716. int rtnl_link_vxlan_get_port_range(struct rtnl_link *link,
  717. struct ifla_vxlan_port_range *range)
  718. {
  719. struct vxlan_info *vxi = link->l_info;
  720. IS_VXLAN_LINK_ASSERT(link);
  721. if (!range)
  722. return -NLE_INVAL;
  723. if (vxi->vxi_mask & VXLAN_HAS_PORT_RANGE)
  724. memcpy(range, &vxi->vxi_port_range, sizeof(*range));
  725. else
  726. return -NLE_AGAIN;
  727. return 0;
  728. }
  729. /**
  730. * Set ARP proxy status to use for VXLAN
  731. * @arg link Link object
  732. * @arg proxy Status value
  733. *
  734. * @return 0 on success or a negative error code
  735. */
  736. int rtnl_link_vxlan_set_proxy(struct rtnl_link *link, uint8_t proxy)
  737. {
  738. struct vxlan_info *vxi = link->l_info;
  739. IS_VXLAN_LINK_ASSERT(link);
  740. vxi->vxi_proxy = proxy;
  741. vxi->vxi_mask |= VXLAN_HAS_PROXY;
  742. return 0;
  743. }
  744. /**
  745. * Get ARP proxy status to use for VXLAN
  746. * @arg link Link object
  747. *
  748. * @return Status value on success or a negative error code
  749. */
  750. int rtnl_link_vxlan_get_proxy(struct rtnl_link *link)
  751. {
  752. struct vxlan_info *vxi = link->l_info;
  753. IS_VXLAN_LINK_ASSERT(link);
  754. if (!(vxi->vxi_mask & VXLAN_HAS_PROXY))
  755. return -NLE_AGAIN;
  756. return vxi->vxi_proxy;
  757. }
  758. /**
  759. * Enable ARP proxy
  760. * @arg link Link object
  761. *
  762. * @return 0 on success or a negative error code
  763. */
  764. int rtnl_link_vxlan_enable_proxy(struct rtnl_link *link)
  765. {
  766. return rtnl_link_vxlan_set_proxy(link, 1);
  767. }
  768. /**
  769. * Disable ARP proxy
  770. * @arg link Link object
  771. *
  772. * @return 0 on success or a negative error code
  773. */
  774. int rtnl_link_vxlan_disable_proxy(struct rtnl_link *link)
  775. {
  776. return rtnl_link_vxlan_set_proxy(link, 0);
  777. }
  778. /**
  779. * Set Route Short Circuit status to use for VXLAN
  780. * @arg link Link object
  781. * @arg rsc Status value
  782. *
  783. * @return 0 on success or a negative error code
  784. */
  785. int rtnl_link_vxlan_set_rsc(struct rtnl_link *link, uint8_t rsc)
  786. {
  787. struct vxlan_info *vxi = link->l_info;
  788. IS_VXLAN_LINK_ASSERT(link);
  789. vxi->vxi_rsc = rsc;
  790. vxi->vxi_mask |= VXLAN_HAS_RSC;
  791. return 0;
  792. }
  793. /**
  794. * Get Route Short Circuit status to use for VXLAN
  795. * @arg link Link object
  796. *
  797. * @return Status value on success or a negative error code
  798. */
  799. int rtnl_link_vxlan_get_rsc(struct rtnl_link *link)
  800. {
  801. struct vxlan_info *vxi = link->l_info;
  802. IS_VXLAN_LINK_ASSERT(link);
  803. if (!(vxi->vxi_mask & VXLAN_HAS_RSC))
  804. return -NLE_AGAIN;
  805. return vxi->vxi_rsc;
  806. }
  807. /**
  808. * Enable Route Short Circuit
  809. * @arg link Link object
  810. *
  811. * @return 0 on success or a negative error code
  812. */
  813. int rtnl_link_vxlan_enable_rsc(struct rtnl_link *link)
  814. {
  815. return rtnl_link_vxlan_set_rsc(link, 1);
  816. }
  817. /**
  818. * Disable Route Short Circuit
  819. * @arg link Link object
  820. *
  821. * @return 0 on success or a negative error code
  822. */
  823. int rtnl_link_vxlan_disable_rsc(struct rtnl_link *link)
  824. {
  825. return rtnl_link_vxlan_set_rsc(link, 0);
  826. }
  827. /**
  828. * Set netlink LLADDR miss notification status to use for VXLAN
  829. * @arg link Link object
  830. * @arg miss Status value
  831. *
  832. * @return 0 on success or a negative error code
  833. */
  834. int rtnl_link_vxlan_set_l2miss(struct rtnl_link *link, uint8_t miss)
  835. {
  836. struct vxlan_info *vxi = link->l_info;
  837. IS_VXLAN_LINK_ASSERT(link);
  838. vxi->vxi_l2miss = miss;
  839. vxi->vxi_mask |= VXLAN_HAS_L2MISS;
  840. return 0;
  841. }
  842. /**
  843. * Get netlink LLADDR miss notification status to use for VXLAN
  844. * @arg link Link object
  845. *
  846. * @return Status value on success or a negative error code
  847. */
  848. int rtnl_link_vxlan_get_l2miss(struct rtnl_link *link)
  849. {
  850. struct vxlan_info *vxi = link->l_info;
  851. IS_VXLAN_LINK_ASSERT(link);
  852. if (!(vxi->vxi_mask & VXLAN_HAS_L2MISS))
  853. return -NLE_AGAIN;
  854. return vxi->vxi_l2miss;
  855. }
  856. /**
  857. * Enable netlink LLADDR miss notifications
  858. * @arg link Link object
  859. *
  860. * @return 0 on success or a negative error code
  861. */
  862. int rtnl_link_vxlan_enable_l2miss(struct rtnl_link *link)
  863. {
  864. return rtnl_link_vxlan_set_l2miss(link, 1);
  865. }
  866. /**
  867. * Disable netlink LLADDR miss notifications
  868. * @arg link Link object
  869. *
  870. * @return 0 on success or a negative error code
  871. */
  872. int rtnl_link_vxlan_disable_l2miss(struct rtnl_link *link)
  873. {
  874. return rtnl_link_vxlan_set_l2miss(link, 0);
  875. }
  876. /**
  877. * Set netlink IP ADDR miss notification status to use for VXLAN
  878. * @arg link Link object
  879. * @arg miss Status value
  880. *
  881. * @return 0 on success or a negative error code
  882. */
  883. int rtnl_link_vxlan_set_l3miss(struct rtnl_link *link, uint8_t miss)
  884. {
  885. struct vxlan_info *vxi = link->l_info;
  886. IS_VXLAN_LINK_ASSERT(link);
  887. vxi->vxi_l3miss = miss;
  888. vxi->vxi_mask |= VXLAN_HAS_L3MISS;
  889. return 0;
  890. }
  891. /**
  892. * Get netlink IP ADDR miss notification status to use for VXLAN
  893. * @arg link Link object
  894. *
  895. * @return Status value on success or a negative error code
  896. */
  897. int rtnl_link_vxlan_get_l3miss(struct rtnl_link *link)
  898. {
  899. struct vxlan_info *vxi = link->l_info;
  900. IS_VXLAN_LINK_ASSERT(link);
  901. if (!(vxi->vxi_mask & VXLAN_HAS_L3MISS))
  902. return -NLE_AGAIN;
  903. return vxi->vxi_l3miss;
  904. }
  905. /**
  906. * Enable netlink IP DDR miss notifications
  907. * @arg link Link object
  908. *
  909. * @return 0 on success or a negative error code
  910. */
  911. int rtnl_link_vxlan_enable_l3miss(struct rtnl_link *link)
  912. {
  913. return rtnl_link_vxlan_set_l3miss(link, 1);
  914. }
  915. /**
  916. * Disable netlink IP ADDR miss notifications
  917. * @arg link Link object
  918. *
  919. * @return 0 on success or a negative error code
  920. */
  921. int rtnl_link_vxlan_disable_l3miss(struct rtnl_link *link)
  922. {
  923. return rtnl_link_vxlan_set_l3miss(link, 0);
  924. }
  925. /** @} */
  926. static void __init vxlan_init(void)
  927. {
  928. rtnl_link_register_info(&vxlan_info_ops);
  929. }
  930. static void __exit vxlan_exit(void)
  931. {
  932. rtnl_link_unregister_info(&vxlan_info_ops);
  933. }
  934. /** @} */