bond_netlink.c 26 KB

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