bond_netlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
  3. * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
  4. * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/if_link.h>
  16. #include <linux/if_ether.h>
  17. #include <net/netlink.h>
  18. #include <net/rtnetlink.h>
  19. #include <net/bonding.h>
  20. static size_t bond_get_slave_size(const struct net_device *bond_dev,
  21. const struct net_device *slave_dev)
  22. {
  23. return nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_STATE */
  24. nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_MII_STATUS */
  25. nla_total_size(sizeof(u32)) + /* IFLA_BOND_SLAVE_LINK_FAILURE_COUNT */
  26. nla_total_size(MAX_ADDR_LEN) + /* IFLA_BOND_SLAVE_PERM_HWADDR */
  27. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_QUEUE_ID */
  28. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
  29. nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
  30. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
  31. 0;
  32. }
  33. static int bond_fill_slave_info(struct sk_buff *skb,
  34. const struct net_device *bond_dev,
  35. const struct net_device *slave_dev)
  36. {
  37. struct slave *slave = bond_slave_get_rtnl(slave_dev);
  38. if (nla_put_u8(skb, IFLA_BOND_SLAVE_STATE, bond_slave_state(slave)))
  39. goto nla_put_failure;
  40. if (nla_put_u8(skb, IFLA_BOND_SLAVE_MII_STATUS, slave->link))
  41. goto nla_put_failure;
  42. if (nla_put_u32(skb, IFLA_BOND_SLAVE_LINK_FAILURE_COUNT,
  43. slave->link_failure_count))
  44. goto nla_put_failure;
  45. if (nla_put(skb, IFLA_BOND_SLAVE_PERM_HWADDR,
  46. slave_dev->addr_len, slave->perm_hwaddr))
  47. goto nla_put_failure;
  48. if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
  49. goto nla_put_failure;
  50. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  51. const struct aggregator *agg;
  52. const struct port *ad_port;
  53. ad_port = &SLAVE_AD_INFO(slave)->port;
  54. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  55. if (agg) {
  56. if (nla_put_u16(skb, IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  57. agg->aggregator_identifier))
  58. goto nla_put_failure;
  59. if (nla_put_u8(skb,
  60. IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  61. ad_port->actor_oper_port_state))
  62. goto nla_put_failure;
  63. if (nla_put_u16(skb,
  64. IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
  65. ad_port->partner_oper.port_state))
  66. goto nla_put_failure;
  67. }
  68. }
  69. return 0;
  70. nla_put_failure:
  71. return -EMSGSIZE;
  72. }
  73. static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
  74. [IFLA_BOND_MODE] = { .type = NLA_U8 },
  75. [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
  76. [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
  77. [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
  78. [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
  79. [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
  80. [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
  81. [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
  82. [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
  83. [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
  84. [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
  85. [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
  86. [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
  87. [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
  88. [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
  89. [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
  90. [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
  91. [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
  92. [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
  93. [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
  94. [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
  95. [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
  96. [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
  97. [IFLA_BOND_AD_ACTOR_SYS_PRIO] = { .type = NLA_U16 },
  98. [IFLA_BOND_AD_USER_PORT_KEY] = { .type = NLA_U16 },
  99. [IFLA_BOND_AD_ACTOR_SYSTEM] = { .type = NLA_BINARY,
  100. .len = ETH_ALEN },
  101. [IFLA_BOND_TLB_DYNAMIC_LB] = { .type = NLA_U8 },
  102. };
  103. static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
  104. [IFLA_BOND_SLAVE_QUEUE_ID] = { .type = NLA_U16 },
  105. };
  106. static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
  107. {
  108. if (tb[IFLA_ADDRESS]) {
  109. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  110. return -EINVAL;
  111. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  112. return -EADDRNOTAVAIL;
  113. }
  114. return 0;
  115. }
  116. static int bond_slave_changelink(struct net_device *bond_dev,
  117. struct net_device *slave_dev,
  118. struct nlattr *tb[], struct nlattr *data[])
  119. {
  120. struct bonding *bond = netdev_priv(bond_dev);
  121. struct bond_opt_value newval;
  122. int err;
  123. if (!data)
  124. return 0;
  125. if (data[IFLA_BOND_SLAVE_QUEUE_ID]) {
  126. u16 queue_id = nla_get_u16(data[IFLA_BOND_SLAVE_QUEUE_ID]);
  127. char queue_id_str[IFNAMSIZ + 7];
  128. /* queue_id option setting expects slave_name:queue_id */
  129. snprintf(queue_id_str, sizeof(queue_id_str), "%s:%u\n",
  130. slave_dev->name, queue_id);
  131. bond_opt_initstr(&newval, queue_id_str);
  132. err = __bond_opt_set(bond, BOND_OPT_QUEUE_ID, &newval);
  133. if (err)
  134. return err;
  135. }
  136. return 0;
  137. }
  138. static int bond_changelink(struct net_device *bond_dev,
  139. struct nlattr *tb[], struct nlattr *data[])
  140. {
  141. struct bonding *bond = netdev_priv(bond_dev);
  142. struct bond_opt_value newval;
  143. int miimon = 0;
  144. int err;
  145. if (!data)
  146. return 0;
  147. if (data[IFLA_BOND_MODE]) {
  148. int mode = nla_get_u8(data[IFLA_BOND_MODE]);
  149. bond_opt_initval(&newval, mode);
  150. err = __bond_opt_set(bond, BOND_OPT_MODE, &newval);
  151. if (err)
  152. return err;
  153. }
  154. if (data[IFLA_BOND_ACTIVE_SLAVE]) {
  155. int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
  156. struct net_device *slave_dev;
  157. char *active_slave = "";
  158. if (ifindex != 0) {
  159. slave_dev = __dev_get_by_index(dev_net(bond_dev),
  160. ifindex);
  161. if (!slave_dev)
  162. return -ENODEV;
  163. active_slave = slave_dev->name;
  164. }
  165. bond_opt_initstr(&newval, active_slave);
  166. err = __bond_opt_set(bond, BOND_OPT_ACTIVE_SLAVE, &newval);
  167. if (err)
  168. return err;
  169. }
  170. if (data[IFLA_BOND_MIIMON]) {
  171. miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
  172. bond_opt_initval(&newval, miimon);
  173. err = __bond_opt_set(bond, BOND_OPT_MIIMON, &newval);
  174. if (err)
  175. return err;
  176. }
  177. if (data[IFLA_BOND_UPDELAY]) {
  178. int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
  179. bond_opt_initval(&newval, updelay);
  180. err = __bond_opt_set(bond, BOND_OPT_UPDELAY, &newval);
  181. if (err)
  182. return err;
  183. }
  184. if (data[IFLA_BOND_DOWNDELAY]) {
  185. int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
  186. bond_opt_initval(&newval, downdelay);
  187. err = __bond_opt_set(bond, BOND_OPT_DOWNDELAY, &newval);
  188. if (err)
  189. return err;
  190. }
  191. if (data[IFLA_BOND_USE_CARRIER]) {
  192. int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
  193. bond_opt_initval(&newval, use_carrier);
  194. err = __bond_opt_set(bond, BOND_OPT_USE_CARRIER, &newval);
  195. if (err)
  196. return err;
  197. }
  198. if (data[IFLA_BOND_ARP_INTERVAL]) {
  199. int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
  200. if (arp_interval && miimon) {
  201. netdev_err(bond->dev, "ARP monitoring cannot be used with MII monitoring\n");
  202. return -EINVAL;
  203. }
  204. bond_opt_initval(&newval, arp_interval);
  205. err = __bond_opt_set(bond, BOND_OPT_ARP_INTERVAL, &newval);
  206. if (err)
  207. return err;
  208. }
  209. if (data[IFLA_BOND_ARP_IP_TARGET]) {
  210. struct nlattr *attr;
  211. int i = 0, rem;
  212. bond_option_arp_ip_targets_clear(bond);
  213. nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
  214. __be32 target;
  215. if (nla_len(attr) < sizeof(target))
  216. return -EINVAL;
  217. target = nla_get_be32(attr);
  218. bond_opt_initval(&newval, (__force u64)target);
  219. err = __bond_opt_set(bond, BOND_OPT_ARP_TARGETS,
  220. &newval);
  221. if (err)
  222. break;
  223. i++;
  224. }
  225. if (i == 0 && bond->params.arp_interval)
  226. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  227. if (err)
  228. return err;
  229. }
  230. if (data[IFLA_BOND_ARP_VALIDATE]) {
  231. int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
  232. if (arp_validate && miimon) {
  233. netdev_err(bond->dev, "ARP validating cannot be used with MII monitoring\n");
  234. return -EINVAL;
  235. }
  236. bond_opt_initval(&newval, arp_validate);
  237. err = __bond_opt_set(bond, BOND_OPT_ARP_VALIDATE, &newval);
  238. if (err)
  239. return err;
  240. }
  241. if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
  242. int arp_all_targets =
  243. nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
  244. bond_opt_initval(&newval, arp_all_targets);
  245. err = __bond_opt_set(bond, BOND_OPT_ARP_ALL_TARGETS, &newval);
  246. if (err)
  247. return err;
  248. }
  249. if (data[IFLA_BOND_PRIMARY]) {
  250. int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
  251. struct net_device *dev;
  252. char *primary = "";
  253. dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
  254. if (dev)
  255. primary = dev->name;
  256. bond_opt_initstr(&newval, primary);
  257. err = __bond_opt_set(bond, BOND_OPT_PRIMARY, &newval);
  258. if (err)
  259. return err;
  260. }
  261. if (data[IFLA_BOND_PRIMARY_RESELECT]) {
  262. int primary_reselect =
  263. nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
  264. bond_opt_initval(&newval, primary_reselect);
  265. err = __bond_opt_set(bond, BOND_OPT_PRIMARY_RESELECT, &newval);
  266. if (err)
  267. return err;
  268. }
  269. if (data[IFLA_BOND_FAIL_OVER_MAC]) {
  270. int fail_over_mac =
  271. nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
  272. bond_opt_initval(&newval, fail_over_mac);
  273. err = __bond_opt_set(bond, BOND_OPT_FAIL_OVER_MAC, &newval);
  274. if (err)
  275. return err;
  276. }
  277. if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
  278. int xmit_hash_policy =
  279. nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
  280. bond_opt_initval(&newval, xmit_hash_policy);
  281. err = __bond_opt_set(bond, BOND_OPT_XMIT_HASH, &newval);
  282. if (err)
  283. return err;
  284. }
  285. if (data[IFLA_BOND_RESEND_IGMP]) {
  286. int resend_igmp =
  287. nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
  288. bond_opt_initval(&newval, resend_igmp);
  289. err = __bond_opt_set(bond, BOND_OPT_RESEND_IGMP, &newval);
  290. if (err)
  291. return err;
  292. }
  293. if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
  294. int num_peer_notif =
  295. nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
  296. bond_opt_initval(&newval, num_peer_notif);
  297. err = __bond_opt_set(bond, BOND_OPT_NUM_PEER_NOTIF, &newval);
  298. if (err)
  299. return err;
  300. }
  301. if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
  302. int all_slaves_active =
  303. nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
  304. bond_opt_initval(&newval, all_slaves_active);
  305. err = __bond_opt_set(bond, BOND_OPT_ALL_SLAVES_ACTIVE, &newval);
  306. if (err)
  307. return err;
  308. }
  309. if (data[IFLA_BOND_MIN_LINKS]) {
  310. int min_links =
  311. nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
  312. bond_opt_initval(&newval, min_links);
  313. err = __bond_opt_set(bond, BOND_OPT_MINLINKS, &newval);
  314. if (err)
  315. return err;
  316. }
  317. if (data[IFLA_BOND_LP_INTERVAL]) {
  318. int lp_interval =
  319. nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
  320. bond_opt_initval(&newval, lp_interval);
  321. err = __bond_opt_set(bond, BOND_OPT_LP_INTERVAL, &newval);
  322. if (err)
  323. return err;
  324. }
  325. if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
  326. int packets_per_slave =
  327. nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
  328. bond_opt_initval(&newval, packets_per_slave);
  329. err = __bond_opt_set(bond, BOND_OPT_PACKETS_PER_SLAVE, &newval);
  330. if (err)
  331. return err;
  332. }
  333. if (data[IFLA_BOND_AD_LACP_RATE]) {
  334. int lacp_rate =
  335. nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
  336. bond_opt_initval(&newval, lacp_rate);
  337. err = __bond_opt_set(bond, BOND_OPT_LACP_RATE, &newval);
  338. if (err)
  339. return err;
  340. }
  341. if (data[IFLA_BOND_AD_SELECT]) {
  342. int ad_select =
  343. nla_get_u8(data[IFLA_BOND_AD_SELECT]);
  344. bond_opt_initval(&newval, ad_select);
  345. err = __bond_opt_set(bond, BOND_OPT_AD_SELECT, &newval);
  346. if (err)
  347. return err;
  348. }
  349. if (data[IFLA_BOND_AD_ACTOR_SYS_PRIO]) {
  350. int actor_sys_prio =
  351. nla_get_u16(data[IFLA_BOND_AD_ACTOR_SYS_PRIO]);
  352. bond_opt_initval(&newval, actor_sys_prio);
  353. err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYS_PRIO, &newval);
  354. if (err)
  355. return err;
  356. }
  357. if (data[IFLA_BOND_AD_USER_PORT_KEY]) {
  358. int port_key =
  359. nla_get_u16(data[IFLA_BOND_AD_USER_PORT_KEY]);
  360. bond_opt_initval(&newval, port_key);
  361. err = __bond_opt_set(bond, BOND_OPT_AD_USER_PORT_KEY, &newval);
  362. if (err)
  363. return err;
  364. }
  365. if (data[IFLA_BOND_AD_ACTOR_SYSTEM]) {
  366. if (nla_len(data[IFLA_BOND_AD_ACTOR_SYSTEM]) != ETH_ALEN)
  367. return -EINVAL;
  368. bond_opt_initval(&newval,
  369. nla_get_be64(data[IFLA_BOND_AD_ACTOR_SYSTEM]));
  370. err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYSTEM, &newval);
  371. if (err)
  372. return err;
  373. }
  374. if (data[IFLA_BOND_TLB_DYNAMIC_LB]) {
  375. int dynamic_lb = nla_get_u8(data[IFLA_BOND_TLB_DYNAMIC_LB]);
  376. bond_opt_initval(&newval, dynamic_lb);
  377. err = __bond_opt_set(bond, BOND_OPT_TLB_DYNAMIC_LB, &newval);
  378. if (err)
  379. return err;
  380. }
  381. return 0;
  382. }
  383. static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
  384. struct nlattr *tb[], struct nlattr *data[])
  385. {
  386. int err;
  387. err = bond_changelink(bond_dev, tb, data);
  388. if (err < 0)
  389. return err;
  390. err = register_netdevice(bond_dev);
  391. netif_carrier_off(bond_dev);
  392. return err;
  393. }
  394. static size_t bond_get_size(const struct net_device *bond_dev)
  395. {
  396. return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
  397. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
  398. nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
  399. nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
  400. nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
  401. nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
  402. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
  403. /* IFLA_BOND_ARP_IP_TARGET */
  404. nla_total_size(sizeof(struct nlattr)) +
  405. nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
  406. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
  407. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
  408. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
  409. nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
  410. nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
  411. nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
  412. nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
  413. nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
  414. nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
  415. nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
  416. nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
  417. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
  418. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
  419. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
  420. nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
  421. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
  422. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
  423. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
  424. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
  425. nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
  426. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */
  427. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */
  428. nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */
  429. nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */
  430. 0;
  431. }
  432. static int bond_option_active_slave_get_ifindex(struct bonding *bond)
  433. {
  434. const struct net_device *slave;
  435. int ifindex;
  436. rcu_read_lock();
  437. slave = bond_option_active_slave_get_rcu(bond);
  438. ifindex = slave ? slave->ifindex : 0;
  439. rcu_read_unlock();
  440. return ifindex;
  441. }
  442. static int bond_fill_info(struct sk_buff *skb,
  443. const struct net_device *bond_dev)
  444. {
  445. struct bonding *bond = netdev_priv(bond_dev);
  446. unsigned int packets_per_slave;
  447. int ifindex, i, targets_added;
  448. struct nlattr *targets;
  449. struct slave *primary;
  450. if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond)))
  451. goto nla_put_failure;
  452. ifindex = bond_option_active_slave_get_ifindex(bond);
  453. if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex))
  454. goto nla_put_failure;
  455. if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
  456. goto nla_put_failure;
  457. if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
  458. bond->params.updelay * bond->params.miimon))
  459. goto nla_put_failure;
  460. if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
  461. bond->params.downdelay * bond->params.miimon))
  462. goto nla_put_failure;
  463. if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
  464. goto nla_put_failure;
  465. if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
  466. goto nla_put_failure;
  467. targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
  468. if (!targets)
  469. goto nla_put_failure;
  470. targets_added = 0;
  471. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  472. if (bond->params.arp_targets[i]) {
  473. nla_put_be32(skb, i, bond->params.arp_targets[i]);
  474. targets_added = 1;
  475. }
  476. }
  477. if (targets_added)
  478. nla_nest_end(skb, targets);
  479. else
  480. nla_nest_cancel(skb, targets);
  481. if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
  482. goto nla_put_failure;
  483. if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
  484. bond->params.arp_all_targets))
  485. goto nla_put_failure;
  486. primary = rtnl_dereference(bond->primary_slave);
  487. if (primary &&
  488. nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex))
  489. goto nla_put_failure;
  490. if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
  491. bond->params.primary_reselect))
  492. goto nla_put_failure;
  493. if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
  494. bond->params.fail_over_mac))
  495. goto nla_put_failure;
  496. if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
  497. bond->params.xmit_policy))
  498. goto nla_put_failure;
  499. if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
  500. bond->params.resend_igmp))
  501. goto nla_put_failure;
  502. if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
  503. bond->params.num_peer_notif))
  504. goto nla_put_failure;
  505. if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
  506. bond->params.all_slaves_active))
  507. goto nla_put_failure;
  508. if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
  509. bond->params.min_links))
  510. goto nla_put_failure;
  511. if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
  512. bond->params.lp_interval))
  513. goto nla_put_failure;
  514. packets_per_slave = bond->params.packets_per_slave;
  515. if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
  516. packets_per_slave))
  517. goto nla_put_failure;
  518. if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
  519. bond->params.lacp_fast))
  520. goto nla_put_failure;
  521. if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
  522. bond->params.ad_select))
  523. goto nla_put_failure;
  524. if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB,
  525. bond->params.tlb_dynamic_lb))
  526. goto nla_put_failure;
  527. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  528. struct ad_info info;
  529. if (capable(CAP_NET_ADMIN)) {
  530. if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO,
  531. bond->params.ad_actor_sys_prio))
  532. goto nla_put_failure;
  533. if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY,
  534. bond->params.ad_user_port_key))
  535. goto nla_put_failure;
  536. if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM,
  537. sizeof(bond->params.ad_actor_system),
  538. &bond->params.ad_actor_system))
  539. goto nla_put_failure;
  540. }
  541. if (!bond_3ad_get_active_agg_info(bond, &info)) {
  542. struct nlattr *nest;
  543. nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
  544. if (!nest)
  545. goto nla_put_failure;
  546. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
  547. info.aggregator_id))
  548. goto nla_put_failure;
  549. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
  550. info.ports))
  551. goto nla_put_failure;
  552. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
  553. info.actor_key))
  554. goto nla_put_failure;
  555. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
  556. info.partner_key))
  557. goto nla_put_failure;
  558. if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
  559. sizeof(info.partner_system),
  560. &info.partner_system))
  561. goto nla_put_failure;
  562. nla_nest_end(skb, nest);
  563. }
  564. }
  565. return 0;
  566. nla_put_failure:
  567. return -EMSGSIZE;
  568. }
  569. struct rtnl_link_ops bond_link_ops __read_mostly = {
  570. .kind = "bond",
  571. .priv_size = sizeof(struct bonding),
  572. .setup = bond_setup,
  573. .maxtype = IFLA_BOND_MAX,
  574. .policy = bond_policy,
  575. .validate = bond_validate,
  576. .newlink = bond_newlink,
  577. .changelink = bond_changelink,
  578. .get_size = bond_get_size,
  579. .fill_info = bond_fill_info,
  580. .get_num_tx_queues = bond_get_num_tx_queues,
  581. .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
  582. as for TX queues */
  583. .slave_maxtype = IFLA_BOND_SLAVE_MAX,
  584. .slave_policy = bond_slave_policy,
  585. .slave_changelink = bond_slave_changelink,
  586. .get_slave_size = bond_get_slave_size,
  587. .fill_slave_info = bond_fill_slave_info,
  588. };
  589. int __init bond_netlink_init(void)
  590. {
  591. return rtnl_link_register(&bond_link_ops);
  592. }
  593. void bond_netlink_fini(void)
  594. {
  595. rtnl_link_unregister(&bond_link_ops);
  596. }
  597. MODULE_ALIAS_RTNL_LINK("bond");