bond_netlink.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
  4. * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
  5. * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/errno.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/if_link.h>
  12. #include <linux/if_ether.h>
  13. #include <net/netlink.h>
  14. #include <net/rtnetlink.h>
  15. #include <net/bonding.h>
  16. #include <net/ipv6.h>
  17. static size_t bond_get_slave_size(const struct net_device *bond_dev,
  18. const struct net_device *slave_dev)
  19. {
  20. return nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_STATE */
  21. nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_MII_STATUS */
  22. nla_total_size(sizeof(u32)) + /* IFLA_BOND_SLAVE_LINK_FAILURE_COUNT */
  23. nla_total_size(MAX_ADDR_LEN) + /* IFLA_BOND_SLAVE_PERM_HWADDR */
  24. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_QUEUE_ID */
  25. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
  26. nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
  27. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
  28. nla_total_size(sizeof(s32)) + /* IFLA_BOND_SLAVE_PRIO */
  29. 0;
  30. }
  31. static int bond_fill_slave_info(struct sk_buff *skb,
  32. const struct net_device *bond_dev,
  33. const struct net_device *slave_dev)
  34. {
  35. struct slave *slave = bond_slave_get_rtnl(slave_dev);
  36. if (nla_put_u8(skb, IFLA_BOND_SLAVE_STATE, bond_slave_state(slave)))
  37. goto nla_put_failure;
  38. if (nla_put_u8(skb, IFLA_BOND_SLAVE_MII_STATUS, slave->link))
  39. goto nla_put_failure;
  40. if (nla_put_u32(skb, IFLA_BOND_SLAVE_LINK_FAILURE_COUNT,
  41. slave->link_failure_count))
  42. goto nla_put_failure;
  43. if (nla_put(skb, IFLA_BOND_SLAVE_PERM_HWADDR,
  44. slave_dev->addr_len, slave->perm_hwaddr))
  45. goto nla_put_failure;
  46. if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID,
  47. READ_ONCE(slave->queue_id)))
  48. goto nla_put_failure;
  49. if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))
  50. goto nla_put_failure;
  51. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  52. const struct aggregator *agg;
  53. const struct port *ad_port;
  54. ad_port = &SLAVE_AD_INFO(slave)->port;
  55. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  56. if (agg) {
  57. if (nla_put_u16(skb, IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  58. agg->aggregator_identifier))
  59. goto nla_put_failure;
  60. if (nla_put_u8(skb,
  61. IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  62. ad_port->actor_oper_port_state))
  63. goto nla_put_failure;
  64. if (nla_put_u16(skb,
  65. IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
  66. ad_port->partner_oper.port_state))
  67. goto nla_put_failure;
  68. }
  69. }
  70. return 0;
  71. nla_put_failure:
  72. return -EMSGSIZE;
  73. }
  74. /* Limit the max delay range to 300s */
  75. static const struct netlink_range_validation delay_range = {
  76. .max = 300000,
  77. };
  78. static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
  79. [IFLA_BOND_MODE] = { .type = NLA_U8 },
  80. [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
  81. [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
  82. [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
  83. [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
  84. [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
  85. [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
  86. [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
  87. [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
  88. [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
  89. [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
  90. [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
  91. [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
  92. [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
  93. [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
  94. [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
  95. [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
  96. [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
  97. [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
  98. [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
  99. [IFLA_BOND_AD_LACP_ACTIVE] = { .type = NLA_U8 },
  100. [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
  101. [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
  102. [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
  103. [IFLA_BOND_AD_ACTOR_SYS_PRIO] = { .type = NLA_U16 },
  104. [IFLA_BOND_AD_USER_PORT_KEY] = { .type = NLA_U16 },
  105. [IFLA_BOND_AD_ACTOR_SYSTEM] = { .type = NLA_BINARY,
  106. .len = ETH_ALEN },
  107. [IFLA_BOND_TLB_DYNAMIC_LB] = { .type = NLA_U8 },
  108. [IFLA_BOND_PEER_NOTIF_DELAY] = NLA_POLICY_FULL_RANGE(NLA_U32, &delay_range),
  109. [IFLA_BOND_MISSED_MAX] = { .type = NLA_U8 },
  110. [IFLA_BOND_NS_IP6_TARGET] = { .type = NLA_NESTED },
  111. [IFLA_BOND_COUPLED_CONTROL] = { .type = NLA_U8 },
  112. };
  113. static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
  114. [IFLA_BOND_SLAVE_QUEUE_ID] = { .type = NLA_U16 },
  115. [IFLA_BOND_SLAVE_PRIO] = { .type = NLA_S32 },
  116. };
  117. static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
  118. struct netlink_ext_ack *extack)
  119. {
  120. if (tb[IFLA_ADDRESS]) {
  121. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  122. return -EINVAL;
  123. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  124. return -EADDRNOTAVAIL;
  125. }
  126. return 0;
  127. }
  128. static int bond_slave_changelink(struct net_device *bond_dev,
  129. struct net_device *slave_dev,
  130. struct nlattr *tb[], struct nlattr *data[],
  131. struct netlink_ext_ack *extack)
  132. {
  133. struct bonding *bond = netdev_priv(bond_dev);
  134. struct bond_opt_value newval;
  135. int err;
  136. if (!data)
  137. return 0;
  138. if (data[IFLA_BOND_SLAVE_QUEUE_ID]) {
  139. u16 queue_id = nla_get_u16(data[IFLA_BOND_SLAVE_QUEUE_ID]);
  140. char queue_id_str[IFNAMSIZ + 7];
  141. /* queue_id option setting expects slave_name:queue_id */
  142. snprintf(queue_id_str, sizeof(queue_id_str), "%s:%u\n",
  143. slave_dev->name, queue_id);
  144. bond_opt_initstr(&newval, queue_id_str);
  145. err = __bond_opt_set(bond, BOND_OPT_QUEUE_ID, &newval,
  146. data[IFLA_BOND_SLAVE_QUEUE_ID], extack);
  147. if (err)
  148. return err;
  149. }
  150. if (data[IFLA_BOND_SLAVE_PRIO]) {
  151. int prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
  152. bond_opt_slave_initval(&newval, &slave_dev, prio);
  153. err = __bond_opt_set(bond, BOND_OPT_PRIO, &newval,
  154. data[IFLA_BOND_SLAVE_PRIO], extack);
  155. if (err)
  156. return err;
  157. }
  158. return 0;
  159. }
  160. static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[],
  161. struct nlattr *data[],
  162. struct netlink_ext_ack *extack)
  163. {
  164. struct bonding *bond = netdev_priv(bond_dev);
  165. struct bond_opt_value newval;
  166. int miimon = 0;
  167. int err;
  168. if (!data)
  169. return 0;
  170. if (data[IFLA_BOND_MODE]) {
  171. int mode = nla_get_u8(data[IFLA_BOND_MODE]);
  172. bond_opt_initval(&newval, mode);
  173. err = __bond_opt_set(bond, BOND_OPT_MODE, &newval,
  174. data[IFLA_BOND_MODE], extack);
  175. if (err)
  176. return err;
  177. }
  178. if (data[IFLA_BOND_ACTIVE_SLAVE]) {
  179. int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
  180. struct net_device *slave_dev;
  181. char *active_slave = "";
  182. if (ifindex != 0) {
  183. slave_dev = __dev_get_by_index(dev_net(bond_dev),
  184. ifindex);
  185. if (!slave_dev)
  186. return -ENODEV;
  187. active_slave = slave_dev->name;
  188. }
  189. bond_opt_initstr(&newval, active_slave);
  190. err = __bond_opt_set(bond, BOND_OPT_ACTIVE_SLAVE, &newval,
  191. data[IFLA_BOND_ACTIVE_SLAVE], extack);
  192. if (err)
  193. return err;
  194. }
  195. if (data[IFLA_BOND_MIIMON]) {
  196. miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
  197. bond_opt_initval(&newval, miimon);
  198. err = __bond_opt_set(bond, BOND_OPT_MIIMON, &newval,
  199. data[IFLA_BOND_MIIMON], extack);
  200. if (err)
  201. return err;
  202. }
  203. if (data[IFLA_BOND_UPDELAY]) {
  204. int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
  205. bond_opt_initval(&newval, updelay);
  206. err = __bond_opt_set(bond, BOND_OPT_UPDELAY, &newval,
  207. data[IFLA_BOND_UPDELAY], extack);
  208. if (err)
  209. return err;
  210. }
  211. if (data[IFLA_BOND_DOWNDELAY]) {
  212. int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
  213. bond_opt_initval(&newval, downdelay);
  214. err = __bond_opt_set(bond, BOND_OPT_DOWNDELAY, &newval,
  215. data[IFLA_BOND_DOWNDELAY], extack);
  216. if (err)
  217. return err;
  218. }
  219. if (data[IFLA_BOND_PEER_NOTIF_DELAY]) {
  220. int delay = nla_get_u32(data[IFLA_BOND_PEER_NOTIF_DELAY]);
  221. bond_opt_initval(&newval, delay);
  222. err = __bond_opt_set(bond, BOND_OPT_PEER_NOTIF_DELAY, &newval,
  223. data[IFLA_BOND_PEER_NOTIF_DELAY], extack);
  224. if (err)
  225. return err;
  226. }
  227. if (data[IFLA_BOND_USE_CARRIER]) {
  228. int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
  229. bond_opt_initval(&newval, use_carrier);
  230. err = __bond_opt_set(bond, BOND_OPT_USE_CARRIER, &newval,
  231. data[IFLA_BOND_USE_CARRIER], extack);
  232. if (err)
  233. return err;
  234. }
  235. if (data[IFLA_BOND_ARP_INTERVAL]) {
  236. int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
  237. if (arp_interval && miimon) {
  238. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL],
  239. "ARP monitoring cannot be used with MII monitoring");
  240. return -EINVAL;
  241. }
  242. bond_opt_initval(&newval, arp_interval);
  243. err = __bond_opt_set(bond, BOND_OPT_ARP_INTERVAL, &newval,
  244. data[IFLA_BOND_ARP_INTERVAL], extack);
  245. if (err)
  246. return err;
  247. }
  248. if (data[IFLA_BOND_ARP_IP_TARGET]) {
  249. struct nlattr *attr;
  250. int i = 0, rem;
  251. bond_option_arp_ip_targets_clear(bond);
  252. nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
  253. __be32 target;
  254. if (nla_len(attr) < sizeof(target))
  255. return -EINVAL;
  256. target = nla_get_be32(attr);
  257. bond_opt_initval(&newval, (__force u64)target);
  258. err = __bond_opt_set(bond, BOND_OPT_ARP_TARGETS,
  259. &newval,
  260. data[IFLA_BOND_ARP_IP_TARGET],
  261. extack);
  262. if (err)
  263. break;
  264. i++;
  265. }
  266. if (i == 0 && bond->params.arp_interval)
  267. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  268. if (err)
  269. return err;
  270. }
  271. #if IS_ENABLED(CONFIG_IPV6)
  272. if (data[IFLA_BOND_NS_IP6_TARGET]) {
  273. struct nlattr *attr;
  274. int i = 0, rem;
  275. bond_option_ns_ip6_targets_clear(bond);
  276. nla_for_each_nested(attr, data[IFLA_BOND_NS_IP6_TARGET], rem) {
  277. struct in6_addr addr6;
  278. if (nla_len(attr) < sizeof(addr6)) {
  279. NL_SET_ERR_MSG(extack, "Invalid IPv6 address");
  280. return -EINVAL;
  281. }
  282. addr6 = nla_get_in6_addr(attr);
  283. bond_opt_initextra(&newval, &addr6, sizeof(addr6));
  284. err = __bond_opt_set(bond, BOND_OPT_NS_TARGETS,
  285. &newval,
  286. data[IFLA_BOND_NS_IP6_TARGET],
  287. extack);
  288. if (err)
  289. break;
  290. i++;
  291. }
  292. if (i == 0 && bond->params.arp_interval)
  293. netdev_warn(bond->dev, "Removing last ns target with arp_interval on\n");
  294. if (err)
  295. return err;
  296. }
  297. #endif
  298. if (data[IFLA_BOND_ARP_VALIDATE]) {
  299. int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
  300. if (arp_validate && miimon) {
  301. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_BOND_ARP_INTERVAL],
  302. "ARP validating cannot be used with MII monitoring");
  303. return -EINVAL;
  304. }
  305. bond_opt_initval(&newval, arp_validate);
  306. err = __bond_opt_set(bond, BOND_OPT_ARP_VALIDATE, &newval,
  307. data[IFLA_BOND_ARP_VALIDATE], extack);
  308. if (err)
  309. return err;
  310. }
  311. if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
  312. int arp_all_targets =
  313. nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
  314. bond_opt_initval(&newval, arp_all_targets);
  315. err = __bond_opt_set(bond, BOND_OPT_ARP_ALL_TARGETS, &newval,
  316. data[IFLA_BOND_ARP_ALL_TARGETS], extack);
  317. if (err)
  318. return err;
  319. }
  320. if (data[IFLA_BOND_PRIMARY]) {
  321. int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
  322. struct net_device *dev;
  323. char *primary = "";
  324. dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
  325. if (dev)
  326. primary = dev->name;
  327. bond_opt_initstr(&newval, primary);
  328. err = __bond_opt_set(bond, BOND_OPT_PRIMARY, &newval,
  329. data[IFLA_BOND_PRIMARY], extack);
  330. if (err)
  331. return err;
  332. }
  333. if (data[IFLA_BOND_PRIMARY_RESELECT]) {
  334. int primary_reselect =
  335. nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
  336. bond_opt_initval(&newval, primary_reselect);
  337. err = __bond_opt_set(bond, BOND_OPT_PRIMARY_RESELECT, &newval,
  338. data[IFLA_BOND_PRIMARY_RESELECT], extack);
  339. if (err)
  340. return err;
  341. }
  342. if (data[IFLA_BOND_FAIL_OVER_MAC]) {
  343. int fail_over_mac =
  344. nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
  345. bond_opt_initval(&newval, fail_over_mac);
  346. err = __bond_opt_set(bond, BOND_OPT_FAIL_OVER_MAC, &newval,
  347. data[IFLA_BOND_FAIL_OVER_MAC], extack);
  348. if (err)
  349. return err;
  350. }
  351. if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
  352. int xmit_hash_policy =
  353. nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
  354. bond_opt_initval(&newval, xmit_hash_policy);
  355. err = __bond_opt_set(bond, BOND_OPT_XMIT_HASH, &newval,
  356. data[IFLA_BOND_XMIT_HASH_POLICY], extack);
  357. if (err)
  358. return err;
  359. }
  360. if (data[IFLA_BOND_RESEND_IGMP]) {
  361. int resend_igmp =
  362. nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
  363. bond_opt_initval(&newval, resend_igmp);
  364. err = __bond_opt_set(bond, BOND_OPT_RESEND_IGMP, &newval,
  365. data[IFLA_BOND_RESEND_IGMP], extack);
  366. if (err)
  367. return err;
  368. }
  369. if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
  370. int num_peer_notif =
  371. nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
  372. bond_opt_initval(&newval, num_peer_notif);
  373. err = __bond_opt_set(bond, BOND_OPT_NUM_PEER_NOTIF, &newval,
  374. data[IFLA_BOND_NUM_PEER_NOTIF], extack);
  375. if (err)
  376. return err;
  377. }
  378. if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
  379. int all_slaves_active =
  380. nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
  381. bond_opt_initval(&newval, all_slaves_active);
  382. err = __bond_opt_set(bond, BOND_OPT_ALL_SLAVES_ACTIVE, &newval,
  383. data[IFLA_BOND_ALL_SLAVES_ACTIVE], extack);
  384. if (err)
  385. return err;
  386. }
  387. if (data[IFLA_BOND_MIN_LINKS]) {
  388. int min_links =
  389. nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
  390. bond_opt_initval(&newval, min_links);
  391. err = __bond_opt_set(bond, BOND_OPT_MINLINKS, &newval,
  392. data[IFLA_BOND_MIN_LINKS], extack);
  393. if (err)
  394. return err;
  395. }
  396. if (data[IFLA_BOND_LP_INTERVAL]) {
  397. int lp_interval =
  398. nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
  399. bond_opt_initval(&newval, lp_interval);
  400. err = __bond_opt_set(bond, BOND_OPT_LP_INTERVAL, &newval,
  401. data[IFLA_BOND_LP_INTERVAL], extack);
  402. if (err)
  403. return err;
  404. }
  405. if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
  406. int packets_per_slave =
  407. nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
  408. bond_opt_initval(&newval, packets_per_slave);
  409. err = __bond_opt_set(bond, BOND_OPT_PACKETS_PER_SLAVE, &newval,
  410. data[IFLA_BOND_PACKETS_PER_SLAVE], extack);
  411. if (err)
  412. return err;
  413. }
  414. if (data[IFLA_BOND_AD_LACP_ACTIVE]) {
  415. int lacp_active = nla_get_u8(data[IFLA_BOND_AD_LACP_ACTIVE]);
  416. bond_opt_initval(&newval, lacp_active);
  417. err = __bond_opt_set(bond, BOND_OPT_LACP_ACTIVE, &newval,
  418. data[IFLA_BOND_AD_LACP_ACTIVE], extack);
  419. if (err)
  420. return err;
  421. }
  422. if (data[IFLA_BOND_AD_LACP_RATE]) {
  423. int lacp_rate =
  424. nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
  425. bond_opt_initval(&newval, lacp_rate);
  426. err = __bond_opt_set(bond, BOND_OPT_LACP_RATE, &newval,
  427. data[IFLA_BOND_AD_LACP_RATE], extack);
  428. if (err)
  429. return err;
  430. }
  431. if (data[IFLA_BOND_AD_SELECT]) {
  432. int ad_select =
  433. nla_get_u8(data[IFLA_BOND_AD_SELECT]);
  434. bond_opt_initval(&newval, ad_select);
  435. err = __bond_opt_set(bond, BOND_OPT_AD_SELECT, &newval,
  436. data[IFLA_BOND_AD_SELECT], extack);
  437. if (err)
  438. return err;
  439. }
  440. if (data[IFLA_BOND_AD_ACTOR_SYS_PRIO]) {
  441. int actor_sys_prio =
  442. nla_get_u16(data[IFLA_BOND_AD_ACTOR_SYS_PRIO]);
  443. bond_opt_initval(&newval, actor_sys_prio);
  444. err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYS_PRIO, &newval,
  445. data[IFLA_BOND_AD_ACTOR_SYS_PRIO], extack);
  446. if (err)
  447. return err;
  448. }
  449. if (data[IFLA_BOND_AD_USER_PORT_KEY]) {
  450. int port_key =
  451. nla_get_u16(data[IFLA_BOND_AD_USER_PORT_KEY]);
  452. bond_opt_initval(&newval, port_key);
  453. err = __bond_opt_set(bond, BOND_OPT_AD_USER_PORT_KEY, &newval,
  454. data[IFLA_BOND_AD_USER_PORT_KEY], extack);
  455. if (err)
  456. return err;
  457. }
  458. if (data[IFLA_BOND_AD_ACTOR_SYSTEM]) {
  459. if (nla_len(data[IFLA_BOND_AD_ACTOR_SYSTEM]) != ETH_ALEN)
  460. return -EINVAL;
  461. bond_opt_initval(&newval,
  462. nla_get_u64(data[IFLA_BOND_AD_ACTOR_SYSTEM]));
  463. err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYSTEM, &newval,
  464. data[IFLA_BOND_AD_ACTOR_SYSTEM], extack);
  465. if (err)
  466. return err;
  467. }
  468. if (data[IFLA_BOND_TLB_DYNAMIC_LB]) {
  469. int dynamic_lb = nla_get_u8(data[IFLA_BOND_TLB_DYNAMIC_LB]);
  470. bond_opt_initval(&newval, dynamic_lb);
  471. err = __bond_opt_set(bond, BOND_OPT_TLB_DYNAMIC_LB, &newval,
  472. data[IFLA_BOND_TLB_DYNAMIC_LB], extack);
  473. if (err)
  474. return err;
  475. }
  476. if (data[IFLA_BOND_MISSED_MAX]) {
  477. int missed_max = nla_get_u8(data[IFLA_BOND_MISSED_MAX]);
  478. bond_opt_initval(&newval, missed_max);
  479. err = __bond_opt_set(bond, BOND_OPT_MISSED_MAX, &newval,
  480. data[IFLA_BOND_MISSED_MAX], extack);
  481. if (err)
  482. return err;
  483. }
  484. if (data[IFLA_BOND_COUPLED_CONTROL]) {
  485. int coupled_control = nla_get_u8(data[IFLA_BOND_COUPLED_CONTROL]);
  486. bond_opt_initval(&newval, coupled_control);
  487. err = __bond_opt_set(bond, BOND_OPT_COUPLED_CONTROL, &newval,
  488. data[IFLA_BOND_COUPLED_CONTROL], extack);
  489. if (err)
  490. return err;
  491. }
  492. return 0;
  493. }
  494. static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
  495. struct nlattr *tb[], struct nlattr *data[],
  496. struct netlink_ext_ack *extack)
  497. {
  498. int err;
  499. err = bond_changelink(bond_dev, tb, data, extack);
  500. if (err < 0)
  501. return err;
  502. err = register_netdevice(bond_dev);
  503. if (!err) {
  504. struct bonding *bond = netdev_priv(bond_dev);
  505. netif_carrier_off(bond_dev);
  506. bond_work_init_all(bond);
  507. }
  508. return err;
  509. }
  510. static size_t bond_get_size(const struct net_device *bond_dev)
  511. {
  512. return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
  513. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
  514. nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
  515. nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
  516. nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
  517. nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
  518. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
  519. /* IFLA_BOND_ARP_IP_TARGET */
  520. nla_total_size(sizeof(struct nlattr)) +
  521. nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
  522. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
  523. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
  524. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
  525. nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
  526. nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
  527. nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
  528. nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
  529. nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
  530. nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
  531. nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
  532. nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
  533. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
  534. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_ACTIVE */
  535. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
  536. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
  537. nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
  538. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
  539. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
  540. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
  541. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
  542. nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
  543. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */
  544. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */
  545. nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */
  546. nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */
  547. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PEER_NOTIF_DELAY */
  548. nla_total_size(sizeof(u8)) + /* IFLA_BOND_MISSED_MAX */
  549. /* IFLA_BOND_NS_IP6_TARGET */
  550. nla_total_size(sizeof(struct nlattr)) +
  551. nla_total_size(sizeof(struct in6_addr)) * BOND_MAX_NS_TARGETS +
  552. nla_total_size(sizeof(u8)) + /* IFLA_BOND_COUPLED_CONTROL */
  553. 0;
  554. }
  555. static int bond_option_active_slave_get_ifindex(struct bonding *bond)
  556. {
  557. const struct net_device *slave;
  558. int ifindex;
  559. rcu_read_lock();
  560. slave = bond_option_active_slave_get_rcu(bond);
  561. ifindex = slave ? slave->ifindex : 0;
  562. rcu_read_unlock();
  563. return ifindex;
  564. }
  565. static int bond_fill_info(struct sk_buff *skb,
  566. const struct net_device *bond_dev)
  567. {
  568. struct bonding *bond = netdev_priv(bond_dev);
  569. unsigned int packets_per_slave;
  570. int ifindex, i, targets_added;
  571. struct nlattr *targets;
  572. struct slave *primary;
  573. if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond)))
  574. goto nla_put_failure;
  575. ifindex = bond_option_active_slave_get_ifindex(bond);
  576. if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex))
  577. goto nla_put_failure;
  578. if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
  579. goto nla_put_failure;
  580. if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
  581. bond->params.updelay * bond->params.miimon))
  582. goto nla_put_failure;
  583. if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
  584. bond->params.downdelay * bond->params.miimon))
  585. goto nla_put_failure;
  586. if (nla_put_u32(skb, IFLA_BOND_PEER_NOTIF_DELAY,
  587. bond->params.peer_notif_delay * bond->params.miimon))
  588. goto nla_put_failure;
  589. if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
  590. goto nla_put_failure;
  591. if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
  592. goto nla_put_failure;
  593. targets = nla_nest_start_noflag(skb, IFLA_BOND_ARP_IP_TARGET);
  594. if (!targets)
  595. goto nla_put_failure;
  596. targets_added = 0;
  597. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  598. if (bond->params.arp_targets[i]) {
  599. if (nla_put_be32(skb, i, bond->params.arp_targets[i]))
  600. goto nla_put_failure;
  601. targets_added = 1;
  602. }
  603. }
  604. if (targets_added)
  605. nla_nest_end(skb, targets);
  606. else
  607. nla_nest_cancel(skb, targets);
  608. if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
  609. goto nla_put_failure;
  610. if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
  611. bond->params.arp_all_targets))
  612. goto nla_put_failure;
  613. #if IS_ENABLED(CONFIG_IPV6)
  614. targets = nla_nest_start(skb, IFLA_BOND_NS_IP6_TARGET);
  615. if (!targets)
  616. goto nla_put_failure;
  617. targets_added = 0;
  618. for (i = 0; i < BOND_MAX_NS_TARGETS; i++) {
  619. if (!ipv6_addr_any(&bond->params.ns_targets[i])) {
  620. if (nla_put_in6_addr(skb, i, &bond->params.ns_targets[i]))
  621. goto nla_put_failure;
  622. targets_added = 1;
  623. }
  624. }
  625. if (targets_added)
  626. nla_nest_end(skb, targets);
  627. else
  628. nla_nest_cancel(skb, targets);
  629. #endif
  630. primary = rtnl_dereference(bond->primary_slave);
  631. if (primary &&
  632. nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex))
  633. goto nla_put_failure;
  634. if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
  635. bond->params.primary_reselect))
  636. goto nla_put_failure;
  637. if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
  638. bond->params.fail_over_mac))
  639. goto nla_put_failure;
  640. if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
  641. bond->params.xmit_policy))
  642. goto nla_put_failure;
  643. if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
  644. bond->params.resend_igmp))
  645. goto nla_put_failure;
  646. if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
  647. bond->params.num_peer_notif))
  648. goto nla_put_failure;
  649. if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
  650. bond->params.all_slaves_active))
  651. goto nla_put_failure;
  652. if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
  653. bond->params.min_links))
  654. goto nla_put_failure;
  655. if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
  656. bond->params.lp_interval))
  657. goto nla_put_failure;
  658. packets_per_slave = bond->params.packets_per_slave;
  659. if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
  660. packets_per_slave))
  661. goto nla_put_failure;
  662. if (nla_put_u8(skb, IFLA_BOND_AD_LACP_ACTIVE,
  663. bond->params.lacp_active))
  664. goto nla_put_failure;
  665. if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
  666. bond->params.lacp_fast))
  667. goto nla_put_failure;
  668. if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
  669. bond->params.ad_select))
  670. goto nla_put_failure;
  671. if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB,
  672. bond->params.tlb_dynamic_lb))
  673. goto nla_put_failure;
  674. if (nla_put_u8(skb, IFLA_BOND_MISSED_MAX,
  675. bond->params.missed_max))
  676. goto nla_put_failure;
  677. if (nla_put_u8(skb, IFLA_BOND_COUPLED_CONTROL,
  678. bond->params.coupled_control))
  679. goto nla_put_failure;
  680. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  681. struct ad_info info;
  682. if (capable(CAP_NET_ADMIN)) {
  683. if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO,
  684. bond->params.ad_actor_sys_prio))
  685. goto nla_put_failure;
  686. if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY,
  687. bond->params.ad_user_port_key))
  688. goto nla_put_failure;
  689. if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM,
  690. ETH_ALEN, &bond->params.ad_actor_system))
  691. goto nla_put_failure;
  692. }
  693. if (!bond_3ad_get_active_agg_info(bond, &info)) {
  694. struct nlattr *nest;
  695. nest = nla_nest_start_noflag(skb, IFLA_BOND_AD_INFO);
  696. if (!nest)
  697. goto nla_put_failure;
  698. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
  699. info.aggregator_id))
  700. goto nla_put_failure;
  701. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
  702. info.ports))
  703. goto nla_put_failure;
  704. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
  705. info.actor_key))
  706. goto nla_put_failure;
  707. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
  708. info.partner_key))
  709. goto nla_put_failure;
  710. if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
  711. sizeof(info.partner_system),
  712. &info.partner_system))
  713. goto nla_put_failure;
  714. nla_nest_end(skb, nest);
  715. }
  716. }
  717. return 0;
  718. nla_put_failure:
  719. return -EMSGSIZE;
  720. }
  721. static size_t bond_get_linkxstats_size(const struct net_device *dev, int attr)
  722. {
  723. switch (attr) {
  724. case IFLA_STATS_LINK_XSTATS:
  725. case IFLA_STATS_LINK_XSTATS_SLAVE:
  726. break;
  727. default:
  728. return 0;
  729. }
  730. return bond_3ad_stats_size() + nla_total_size(0);
  731. }
  732. static int bond_fill_linkxstats(struct sk_buff *skb,
  733. const struct net_device *dev,
  734. int *prividx, int attr)
  735. {
  736. struct nlattr *nla __maybe_unused;
  737. struct slave *slave = NULL;
  738. struct nlattr *nest, *nest2;
  739. struct bonding *bond;
  740. switch (attr) {
  741. case IFLA_STATS_LINK_XSTATS:
  742. bond = netdev_priv(dev);
  743. break;
  744. case IFLA_STATS_LINK_XSTATS_SLAVE:
  745. slave = bond_slave_get_rtnl(dev);
  746. if (!slave)
  747. return 0;
  748. bond = slave->bond;
  749. break;
  750. default:
  751. return -EINVAL;
  752. }
  753. nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BOND);
  754. if (!nest)
  755. return -EMSGSIZE;
  756. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  757. struct bond_3ad_stats *stats;
  758. if (slave)
  759. stats = &SLAVE_AD_INFO(slave)->stats;
  760. else
  761. stats = &BOND_AD_INFO(bond).stats;
  762. nest2 = nla_nest_start_noflag(skb, BOND_XSTATS_3AD);
  763. if (!nest2) {
  764. nla_nest_end(skb, nest);
  765. return -EMSGSIZE;
  766. }
  767. if (bond_3ad_stats_fill(skb, stats)) {
  768. nla_nest_cancel(skb, nest2);
  769. nla_nest_end(skb, nest);
  770. return -EMSGSIZE;
  771. }
  772. nla_nest_end(skb, nest2);
  773. }
  774. nla_nest_end(skb, nest);
  775. return 0;
  776. }
  777. struct rtnl_link_ops bond_link_ops __read_mostly = {
  778. .kind = "bond",
  779. .priv_size = sizeof(struct bonding),
  780. .setup = bond_setup,
  781. .maxtype = IFLA_BOND_MAX,
  782. .policy = bond_policy,
  783. .validate = bond_validate,
  784. .newlink = bond_newlink,
  785. .changelink = bond_changelink,
  786. .get_size = bond_get_size,
  787. .fill_info = bond_fill_info,
  788. .get_num_tx_queues = bond_get_num_tx_queues,
  789. .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
  790. as for TX queues */
  791. .fill_linkxstats = bond_fill_linkxstats,
  792. .get_linkxstats_size = bond_get_linkxstats_size,
  793. .slave_maxtype = IFLA_BOND_SLAVE_MAX,
  794. .slave_policy = bond_slave_policy,
  795. .slave_changelink = bond_slave_changelink,
  796. .get_slave_size = bond_get_slave_size,
  797. .fill_slave_info = bond_fill_slave_info,
  798. };
  799. int __init bond_netlink_init(void)
  800. {
  801. return rtnl_link_register(&bond_link_ops);
  802. }
  803. void bond_netlink_fini(void)
  804. {
  805. rtnl_link_unregister(&bond_link_ops);
  806. }
  807. MODULE_ALIAS_RTNL_LINK("bond");