swconfig.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * swconfig.c: Switch configuration API
  3. *
  4. * Copyright (C) 2008 Felix Fietkau <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/if.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/capability.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/switch.h>
  25. #include <linux/of.h>
  26. #include <linux/version.h>
  27. #include <uapi/linux/mii.h>
  28. #define SWCONFIG_DEVNAME "switch%d"
  29. #include "swconfig_leds.c"
  30. MODULE_AUTHOR("Felix Fietkau <[email protected]>");
  31. MODULE_LICENSE("GPL");
  32. static int swdev_id;
  33. static struct list_head swdevs;
  34. static DEFINE_MUTEX(swdevs_lock);
  35. struct swconfig_callback;
  36. struct swconfig_callback {
  37. struct sk_buff *msg;
  38. struct genlmsghdr *hdr;
  39. struct genl_info *info;
  40. int cmd;
  41. /* callback for filling in the message data */
  42. int (*fill)(struct swconfig_callback *cb, void *arg);
  43. /* callback for closing the message before sending it */
  44. int (*close)(struct swconfig_callback *cb, void *arg);
  45. struct nlattr *nest[4];
  46. int args[4];
  47. };
  48. /* defaults */
  49. static int
  50. swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
  51. struct switch_val *val)
  52. {
  53. int ret;
  54. if (val->port_vlan >= dev->vlans)
  55. return -EINVAL;
  56. if (!dev->ops->get_vlan_ports)
  57. return -EOPNOTSUPP;
  58. ret = dev->ops->get_vlan_ports(dev, val);
  59. return ret;
  60. }
  61. static int
  62. swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
  63. struct switch_val *val)
  64. {
  65. struct switch_port *ports = val->value.ports;
  66. const struct switch_dev_ops *ops = dev->ops;
  67. int i;
  68. if (val->port_vlan >= dev->vlans)
  69. return -EINVAL;
  70. /* validate ports */
  71. if (val->len > dev->ports)
  72. return -EINVAL;
  73. if (!ops->set_vlan_ports)
  74. return -EOPNOTSUPP;
  75. for (i = 0; i < val->len; i++) {
  76. if (ports[i].id >= dev->ports)
  77. return -EINVAL;
  78. if (ops->set_port_pvid &&
  79. !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
  80. ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
  81. }
  82. return ops->set_vlan_ports(dev, val);
  83. }
  84. static int
  85. swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr,
  86. struct switch_val *val)
  87. {
  88. if (val->port_vlan >= dev->ports)
  89. return -EINVAL;
  90. if (!dev->ops->set_port_pvid)
  91. return -EOPNOTSUPP;
  92. return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
  93. }
  94. static int
  95. swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
  96. struct switch_val *val)
  97. {
  98. if (val->port_vlan >= dev->ports)
  99. return -EINVAL;
  100. if (!dev->ops->get_port_pvid)
  101. return -EOPNOTSUPP;
  102. return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
  103. }
  104. static int
  105. swconfig_set_link(struct switch_dev *dev, const struct switch_attr *attr,
  106. struct switch_val *val)
  107. {
  108. if (!dev->ops->set_port_link)
  109. return -EOPNOTSUPP;
  110. return dev->ops->set_port_link(dev, val->port_vlan, val->value.link);
  111. }
  112. static int
  113. swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
  114. struct switch_val *val)
  115. {
  116. struct switch_port_link *link = val->value.link;
  117. if (val->port_vlan >= dev->ports)
  118. return -EINVAL;
  119. if (!dev->ops->get_port_link)
  120. return -EOPNOTSUPP;
  121. memset(link, 0, sizeof(*link));
  122. return dev->ops->get_port_link(dev, val->port_vlan, link);
  123. }
  124. static int
  125. swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr,
  126. struct switch_val *val)
  127. {
  128. /* don't complain if not supported by the switch driver */
  129. if (!dev->ops->apply_config)
  130. return 0;
  131. return dev->ops->apply_config(dev);
  132. }
  133. static int
  134. swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr,
  135. struct switch_val *val)
  136. {
  137. /* don't complain if not supported by the switch driver */
  138. if (!dev->ops->reset_switch)
  139. return 0;
  140. return dev->ops->reset_switch(dev);
  141. }
  142. enum global_defaults {
  143. GLOBAL_APPLY,
  144. GLOBAL_RESET,
  145. };
  146. enum vlan_defaults {
  147. VLAN_PORTS,
  148. };
  149. enum port_defaults {
  150. PORT_PVID,
  151. PORT_LINK,
  152. };
  153. static struct switch_attr default_global[] = {
  154. [GLOBAL_APPLY] = {
  155. .type = SWITCH_TYPE_NOVAL,
  156. .name = "apply",
  157. .description = "Activate changes in the hardware",
  158. .set = swconfig_apply_config,
  159. },
  160. [GLOBAL_RESET] = {
  161. .type = SWITCH_TYPE_NOVAL,
  162. .name = "reset",
  163. .description = "Reset the switch",
  164. .set = swconfig_reset_switch,
  165. }
  166. };
  167. static struct switch_attr default_port[] = {
  168. [PORT_PVID] = {
  169. .type = SWITCH_TYPE_INT,
  170. .name = "pvid",
  171. .description = "Primary VLAN ID",
  172. .set = swconfig_set_pvid,
  173. .get = swconfig_get_pvid,
  174. },
  175. [PORT_LINK] = {
  176. .type = SWITCH_TYPE_LINK,
  177. .name = "link",
  178. .description = "Get port link information",
  179. .set = swconfig_set_link,
  180. .get = swconfig_get_link,
  181. }
  182. };
  183. static struct switch_attr default_vlan[] = {
  184. [VLAN_PORTS] = {
  185. .type = SWITCH_TYPE_PORTS,
  186. .name = "ports",
  187. .description = "VLAN port mapping",
  188. .set = swconfig_set_vlan_ports,
  189. .get = swconfig_get_vlan_ports,
  190. },
  191. };
  192. static const struct switch_attr *
  193. swconfig_find_attr_by_name(const struct switch_attrlist *alist,
  194. const char *name)
  195. {
  196. int i;
  197. for (i = 0; i < alist->n_attr; i++)
  198. if (strcmp(name, alist->attr[i].name) == 0)
  199. return &alist->attr[i];
  200. return NULL;
  201. }
  202. static void swconfig_defaults_init(struct switch_dev *dev)
  203. {
  204. const struct switch_dev_ops *ops = dev->ops;
  205. dev->def_global = 0;
  206. dev->def_vlan = 0;
  207. dev->def_port = 0;
  208. if (ops->get_vlan_ports || ops->set_vlan_ports)
  209. set_bit(VLAN_PORTS, &dev->def_vlan);
  210. if (ops->get_port_pvid || ops->set_port_pvid)
  211. set_bit(PORT_PVID, &dev->def_port);
  212. if (ops->get_port_link &&
  213. !swconfig_find_attr_by_name(&ops->attr_port, "link"))
  214. set_bit(PORT_LINK, &dev->def_port);
  215. /* always present, can be no-op */
  216. set_bit(GLOBAL_APPLY, &dev->def_global);
  217. set_bit(GLOBAL_RESET, &dev->def_global);
  218. }
  219. static struct genl_family switch_fam;
  220. static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
  221. [SWITCH_ATTR_ID] = { .type = NLA_U32 },
  222. [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
  223. [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
  224. [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
  225. [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
  226. [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
  227. [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
  228. [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
  229. };
  230. static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
  231. [SWITCH_PORT_ID] = { .type = NLA_U32 },
  232. [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
  233. };
  234. static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
  235. [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
  236. [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
  237. [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
  238. };
  239. static inline void
  240. swconfig_lock(void)
  241. {
  242. mutex_lock(&swdevs_lock);
  243. }
  244. static inline void
  245. swconfig_unlock(void)
  246. {
  247. mutex_unlock(&swdevs_lock);
  248. }
  249. static struct switch_dev *
  250. swconfig_get_dev(struct genl_info *info)
  251. {
  252. struct switch_dev *dev = NULL;
  253. struct switch_dev *p;
  254. int id;
  255. if (!info->attrs[SWITCH_ATTR_ID])
  256. goto done;
  257. id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
  258. swconfig_lock();
  259. list_for_each_entry(p, &swdevs, dev_list) {
  260. if (id != p->id)
  261. continue;
  262. dev = p;
  263. break;
  264. }
  265. if (dev)
  266. mutex_lock(&dev->sw_mutex);
  267. else
  268. pr_debug("device %d not found\n", id);
  269. swconfig_unlock();
  270. done:
  271. return dev;
  272. }
  273. static inline void
  274. swconfig_put_dev(struct switch_dev *dev)
  275. {
  276. mutex_unlock(&dev->sw_mutex);
  277. }
  278. static int
  279. swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
  280. {
  281. struct switch_attr *op = arg;
  282. struct genl_info *info = cb->info;
  283. struct sk_buff *msg = cb->msg;
  284. int id = cb->args[0];
  285. void *hdr;
  286. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
  287. NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
  288. if (IS_ERR(hdr))
  289. return -1;
  290. if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id))
  291. goto nla_put_failure;
  292. if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type))
  293. goto nla_put_failure;
  294. if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name))
  295. goto nla_put_failure;
  296. if (op->description)
  297. if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION,
  298. op->description))
  299. goto nla_put_failure;
  300. genlmsg_end(msg, hdr);
  301. return msg->len;
  302. nla_put_failure:
  303. genlmsg_cancel(msg, hdr);
  304. return -EMSGSIZE;
  305. }
  306. /* spread multipart messages across multiple message buffers */
  307. static int
  308. swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
  309. {
  310. struct genl_info *info = cb->info;
  311. int restart = 0;
  312. int err;
  313. do {
  314. if (!cb->msg) {
  315. cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  316. if (cb->msg == NULL)
  317. goto error;
  318. }
  319. if (!(cb->fill(cb, arg) < 0))
  320. break;
  321. /* fill failed, check if this was already the second attempt */
  322. if (restart)
  323. goto error;
  324. /* try again in a new message, send the current one */
  325. restart = 1;
  326. if (cb->close) {
  327. if (cb->close(cb, arg) < 0)
  328. goto error;
  329. }
  330. err = genlmsg_reply(cb->msg, info);
  331. cb->msg = NULL;
  332. if (err < 0)
  333. goto error;
  334. } while (restart);
  335. return 0;
  336. error:
  337. if (cb->msg)
  338. nlmsg_free(cb->msg);
  339. return -1;
  340. }
  341. static int
  342. swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
  343. {
  344. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  345. const struct switch_attrlist *alist;
  346. struct switch_dev *dev;
  347. struct swconfig_callback cb;
  348. int err = -EINVAL;
  349. int i;
  350. /* defaults */
  351. struct switch_attr *def_list;
  352. unsigned long *def_active;
  353. int n_def;
  354. dev = swconfig_get_dev(info);
  355. if (!dev)
  356. return -EINVAL;
  357. switch (hdr->cmd) {
  358. case SWITCH_CMD_LIST_GLOBAL:
  359. alist = &dev->ops->attr_global;
  360. def_list = default_global;
  361. def_active = &dev->def_global;
  362. n_def = ARRAY_SIZE(default_global);
  363. break;
  364. case SWITCH_CMD_LIST_VLAN:
  365. alist = &dev->ops->attr_vlan;
  366. def_list = default_vlan;
  367. def_active = &dev->def_vlan;
  368. n_def = ARRAY_SIZE(default_vlan);
  369. break;
  370. case SWITCH_CMD_LIST_PORT:
  371. alist = &dev->ops->attr_port;
  372. def_list = default_port;
  373. def_active = &dev->def_port;
  374. n_def = ARRAY_SIZE(default_port);
  375. break;
  376. default:
  377. WARN_ON(1);
  378. goto out;
  379. }
  380. memset(&cb, 0, sizeof(cb));
  381. cb.info = info;
  382. cb.fill = swconfig_dump_attr;
  383. for (i = 0; i < alist->n_attr; i++) {
  384. if (alist->attr[i].disabled)
  385. continue;
  386. cb.args[0] = i;
  387. err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
  388. if (err < 0)
  389. goto error;
  390. }
  391. /* defaults */
  392. for (i = 0; i < n_def; i++) {
  393. if (!test_bit(i, def_active))
  394. continue;
  395. cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
  396. err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
  397. if (err < 0)
  398. goto error;
  399. }
  400. swconfig_put_dev(dev);
  401. if (!cb.msg)
  402. return 0;
  403. return genlmsg_reply(cb.msg, info);
  404. error:
  405. if (cb.msg)
  406. nlmsg_free(cb.msg);
  407. out:
  408. swconfig_put_dev(dev);
  409. return err;
  410. }
  411. static const struct switch_attr *
  412. swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
  413. struct switch_val *val)
  414. {
  415. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  416. const struct switch_attrlist *alist;
  417. const struct switch_attr *attr = NULL;
  418. unsigned int attr_id;
  419. /* defaults */
  420. struct switch_attr *def_list;
  421. unsigned long *def_active;
  422. int n_def;
  423. if (!info->attrs[SWITCH_ATTR_OP_ID])
  424. goto done;
  425. switch (hdr->cmd) {
  426. case SWITCH_CMD_SET_GLOBAL:
  427. case SWITCH_CMD_GET_GLOBAL:
  428. alist = &dev->ops->attr_global;
  429. def_list = default_global;
  430. def_active = &dev->def_global;
  431. n_def = ARRAY_SIZE(default_global);
  432. break;
  433. case SWITCH_CMD_SET_VLAN:
  434. case SWITCH_CMD_GET_VLAN:
  435. alist = &dev->ops->attr_vlan;
  436. def_list = default_vlan;
  437. def_active = &dev->def_vlan;
  438. n_def = ARRAY_SIZE(default_vlan);
  439. if (!info->attrs[SWITCH_ATTR_OP_VLAN])
  440. goto done;
  441. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
  442. if (val->port_vlan >= dev->vlans)
  443. goto done;
  444. break;
  445. case SWITCH_CMD_SET_PORT:
  446. case SWITCH_CMD_GET_PORT:
  447. alist = &dev->ops->attr_port;
  448. def_list = default_port;
  449. def_active = &dev->def_port;
  450. n_def = ARRAY_SIZE(default_port);
  451. if (!info->attrs[SWITCH_ATTR_OP_PORT])
  452. goto done;
  453. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
  454. if (val->port_vlan >= dev->ports)
  455. goto done;
  456. break;
  457. default:
  458. WARN_ON(1);
  459. goto done;
  460. }
  461. if (!alist)
  462. goto done;
  463. attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
  464. if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
  465. attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
  466. if (attr_id >= n_def)
  467. goto done;
  468. if (!test_bit(attr_id, def_active))
  469. goto done;
  470. attr = &def_list[attr_id];
  471. } else {
  472. if (attr_id >= alist->n_attr)
  473. goto done;
  474. attr = &alist->attr[attr_id];
  475. }
  476. if (attr->disabled)
  477. attr = NULL;
  478. done:
  479. if (!attr)
  480. pr_debug("attribute lookup failed\n");
  481. val->attr = attr;
  482. return attr;
  483. }
  484. static int
  485. swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
  486. struct switch_val *val, int max)
  487. {
  488. struct nlattr *nla;
  489. int rem;
  490. val->len = 0;
  491. nla_for_each_nested(nla, head, rem) {
  492. struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
  493. struct switch_port *port;
  494. if (val->len >= max)
  495. return -EINVAL;
  496. port = &val->value.ports[val->len];
  497. if (nla_parse_nested_deprecated(tb, SWITCH_PORT_ATTR_MAX, nla,
  498. port_policy, NULL))
  499. return -EINVAL;
  500. if (!tb[SWITCH_PORT_ID])
  501. return -EINVAL;
  502. port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
  503. if (tb[SWITCH_PORT_FLAG_TAGGED])
  504. port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
  505. val->len++;
  506. }
  507. return 0;
  508. }
  509. static int
  510. swconfig_parse_link(struct sk_buff *msg, struct nlattr *nla,
  511. struct switch_port_link *link)
  512. {
  513. struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
  514. if (nla_parse_nested_deprecated(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy, NULL))
  515. return -EINVAL;
  516. link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
  517. link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
  518. link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
  519. return 0;
  520. }
  521. static int
  522. swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
  523. {
  524. const struct switch_attr *attr;
  525. struct switch_dev *dev;
  526. struct switch_val val;
  527. int err = -EINVAL;
  528. if (!capable(CAP_NET_ADMIN))
  529. return -EPERM;
  530. dev = swconfig_get_dev(info);
  531. if (!dev)
  532. return -EINVAL;
  533. memset(&val, 0, sizeof(val));
  534. attr = swconfig_lookup_attr(dev, info, &val);
  535. if (!attr || !attr->set)
  536. goto error;
  537. val.attr = attr;
  538. switch (attr->type) {
  539. case SWITCH_TYPE_NOVAL:
  540. break;
  541. case SWITCH_TYPE_INT:
  542. if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
  543. goto error;
  544. val.value.i =
  545. nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
  546. break;
  547. case SWITCH_TYPE_STRING:
  548. if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
  549. goto error;
  550. val.value.s =
  551. nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
  552. break;
  553. case SWITCH_TYPE_PORTS:
  554. val.value.ports = dev->portbuf;
  555. memset(dev->portbuf, 0,
  556. sizeof(struct switch_port) * dev->ports);
  557. /* TODO: implement multipart? */
  558. if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
  559. err = swconfig_parse_ports(skb,
  560. info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
  561. &val, dev->ports);
  562. if (err < 0)
  563. goto error;
  564. } else {
  565. val.len = 0;
  566. err = 0;
  567. }
  568. break;
  569. case SWITCH_TYPE_LINK:
  570. val.value.link = &dev->linkbuf;
  571. memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
  572. if (info->attrs[SWITCH_ATTR_OP_VALUE_LINK]) {
  573. err = swconfig_parse_link(skb,
  574. info->attrs[SWITCH_ATTR_OP_VALUE_LINK],
  575. val.value.link);
  576. if (err < 0)
  577. goto error;
  578. } else {
  579. val.len = 0;
  580. err = 0;
  581. }
  582. break;
  583. default:
  584. goto error;
  585. }
  586. err = attr->set(dev, attr, &val);
  587. error:
  588. swconfig_put_dev(dev);
  589. return err;
  590. }
  591. static int
  592. swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
  593. {
  594. if (cb->nest[0])
  595. nla_nest_end(cb->msg, cb->nest[0]);
  596. return 0;
  597. }
  598. static int
  599. swconfig_send_port(struct swconfig_callback *cb, void *arg)
  600. {
  601. const struct switch_port *port = arg;
  602. struct nlattr *p = NULL;
  603. if (!cb->nest[0]) {
  604. cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
  605. if (!cb->nest[0])
  606. return -1;
  607. }
  608. p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
  609. if (!p)
  610. goto error;
  611. if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id))
  612. goto nla_put_failure;
  613. if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  614. if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED))
  615. goto nla_put_failure;
  616. }
  617. nla_nest_end(cb->msg, p);
  618. return 0;
  619. nla_put_failure:
  620. nla_nest_cancel(cb->msg, p);
  621. error:
  622. nla_nest_cancel(cb->msg, cb->nest[0]);
  623. return -1;
  624. }
  625. static int
  626. swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
  627. const struct switch_val *val)
  628. {
  629. struct swconfig_callback cb;
  630. int err = 0;
  631. int i;
  632. if (!val->value.ports)
  633. return -EINVAL;
  634. memset(&cb, 0, sizeof(cb));
  635. cb.cmd = attr;
  636. cb.msg = *msg;
  637. cb.info = info;
  638. cb.fill = swconfig_send_port;
  639. cb.close = swconfig_close_portlist;
  640. cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
  641. for (i = 0; i < val->len; i++) {
  642. err = swconfig_send_multipart(&cb, &val->value.ports[i]);
  643. if (err)
  644. goto done;
  645. }
  646. err = val->len;
  647. swconfig_close_portlist(&cb, NULL);
  648. *msg = cb.msg;
  649. done:
  650. return err;
  651. }
  652. static int
  653. swconfig_send_link(struct sk_buff *msg, struct genl_info *info, int attr,
  654. const struct switch_port_link *link)
  655. {
  656. struct nlattr *p = NULL;
  657. int err = 0;
  658. p = nla_nest_start(msg, attr);
  659. if (link->link) {
  660. if (nla_put_flag(msg, SWITCH_LINK_FLAG_LINK))
  661. goto nla_put_failure;
  662. }
  663. if (link->duplex) {
  664. if (nla_put_flag(msg, SWITCH_LINK_FLAG_DUPLEX))
  665. goto nla_put_failure;
  666. }
  667. if (link->aneg) {
  668. if (nla_put_flag(msg, SWITCH_LINK_FLAG_ANEG))
  669. goto nla_put_failure;
  670. }
  671. if (link->tx_flow) {
  672. if (nla_put_flag(msg, SWITCH_LINK_FLAG_TX_FLOW))
  673. goto nla_put_failure;
  674. }
  675. if (link->rx_flow) {
  676. if (nla_put_flag(msg, SWITCH_LINK_FLAG_RX_FLOW))
  677. goto nla_put_failure;
  678. }
  679. if (nla_put_u32(msg, SWITCH_LINK_SPEED, link->speed))
  680. goto nla_put_failure;
  681. if (link->eee & ADVERTISED_100baseT_Full) {
  682. if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_100BASET))
  683. goto nla_put_failure;
  684. }
  685. if (link->eee & ADVERTISED_1000baseT_Full) {
  686. if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_1000BASET))
  687. goto nla_put_failure;
  688. }
  689. nla_nest_end(msg, p);
  690. return err;
  691. nla_put_failure:
  692. nla_nest_cancel(msg, p);
  693. return -1;
  694. }
  695. static int
  696. swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
  697. {
  698. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  699. const struct switch_attr *attr;
  700. struct switch_dev *dev;
  701. struct sk_buff *msg = NULL;
  702. struct switch_val val;
  703. int err = -EINVAL;
  704. int cmd = hdr->cmd;
  705. dev = swconfig_get_dev(info);
  706. if (!dev)
  707. return -EINVAL;
  708. memset(&val, 0, sizeof(val));
  709. attr = swconfig_lookup_attr(dev, info, &val);
  710. if (!attr || !attr->get)
  711. goto error;
  712. if (attr->type == SWITCH_TYPE_PORTS) {
  713. val.value.ports = dev->portbuf;
  714. memset(dev->portbuf, 0,
  715. sizeof(struct switch_port) * dev->ports);
  716. } else if (attr->type == SWITCH_TYPE_LINK) {
  717. val.value.link = &dev->linkbuf;
  718. memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
  719. }
  720. err = attr->get(dev, attr, &val);
  721. if (err)
  722. goto error;
  723. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  724. if (!msg)
  725. goto error;
  726. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
  727. 0, cmd);
  728. if (IS_ERR(hdr))
  729. goto nla_put_failure;
  730. switch (attr->type) {
  731. case SWITCH_TYPE_INT:
  732. if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
  733. goto nla_put_failure;
  734. break;
  735. case SWITCH_TYPE_STRING:
  736. if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s))
  737. goto nla_put_failure;
  738. break;
  739. case SWITCH_TYPE_PORTS:
  740. err = swconfig_send_ports(&msg, info,
  741. SWITCH_ATTR_OP_VALUE_PORTS, &val);
  742. if (err < 0)
  743. goto nla_put_failure;
  744. break;
  745. case SWITCH_TYPE_LINK:
  746. err = swconfig_send_link(msg, info,
  747. SWITCH_ATTR_OP_VALUE_LINK, val.value.link);
  748. if (err < 0)
  749. goto nla_put_failure;
  750. break;
  751. default:
  752. pr_debug("invalid type in attribute\n");
  753. err = -EINVAL;
  754. goto nla_put_failure;
  755. }
  756. genlmsg_end(msg, hdr);
  757. err = msg->len;
  758. if (err < 0)
  759. goto nla_put_failure;
  760. swconfig_put_dev(dev);
  761. return genlmsg_reply(msg, info);
  762. nla_put_failure:
  763. if (msg)
  764. nlmsg_free(msg);
  765. error:
  766. swconfig_put_dev(dev);
  767. if (!err)
  768. err = -ENOMEM;
  769. return err;
  770. }
  771. static int
  772. swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
  773. const struct switch_dev *dev)
  774. {
  775. struct nlattr *p = NULL, *m = NULL;
  776. void *hdr;
  777. int i;
  778. hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
  779. SWITCH_CMD_NEW_ATTR);
  780. if (IS_ERR(hdr))
  781. return -1;
  782. if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id))
  783. goto nla_put_failure;
  784. if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname))
  785. goto nla_put_failure;
  786. if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias))
  787. goto nla_put_failure;
  788. if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name))
  789. goto nla_put_failure;
  790. if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans))
  791. goto nla_put_failure;
  792. if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports))
  793. goto nla_put_failure;
  794. if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
  795. goto nla_put_failure;
  796. m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
  797. if (!m)
  798. goto nla_put_failure;
  799. for (i = 0; i < dev->ports; i++) {
  800. p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
  801. if (!p)
  802. continue;
  803. if (dev->portmap[i].s) {
  804. if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
  805. dev->portmap[i].s))
  806. goto nla_put_failure;
  807. if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
  808. dev->portmap[i].virt))
  809. goto nla_put_failure;
  810. }
  811. nla_nest_end(msg, p);
  812. }
  813. nla_nest_end(msg, m);
  814. genlmsg_end(msg, hdr);
  815. return msg->len;
  816. nla_put_failure:
  817. genlmsg_cancel(msg, hdr);
  818. return -EMSGSIZE;
  819. }
  820. static int swconfig_dump_switches(struct sk_buff *skb,
  821. struct netlink_callback *cb)
  822. {
  823. struct switch_dev *dev;
  824. int start = cb->args[0];
  825. int idx = 0;
  826. swconfig_lock();
  827. list_for_each_entry(dev, &swdevs, dev_list) {
  828. if (++idx <= start)
  829. continue;
  830. if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid,
  831. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  832. dev) < 0)
  833. break;
  834. }
  835. swconfig_unlock();
  836. cb->args[0] = idx;
  837. return skb->len;
  838. }
  839. static int
  840. swconfig_done(struct netlink_callback *cb)
  841. {
  842. return 0;
  843. }
  844. static struct genl_ops swconfig_ops[] = {
  845. {
  846. .cmd = SWITCH_CMD_LIST_GLOBAL,
  847. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  848. .doit = swconfig_list_attrs,
  849. },
  850. {
  851. .cmd = SWITCH_CMD_LIST_VLAN,
  852. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  853. .doit = swconfig_list_attrs,
  854. },
  855. {
  856. .cmd = SWITCH_CMD_LIST_PORT,
  857. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  858. .doit = swconfig_list_attrs,
  859. },
  860. {
  861. .cmd = SWITCH_CMD_GET_GLOBAL,
  862. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  863. .doit = swconfig_get_attr,
  864. },
  865. {
  866. .cmd = SWITCH_CMD_GET_VLAN,
  867. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  868. .doit = swconfig_get_attr,
  869. },
  870. {
  871. .cmd = SWITCH_CMD_GET_PORT,
  872. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  873. .doit = swconfig_get_attr,
  874. },
  875. {
  876. .cmd = SWITCH_CMD_SET_GLOBAL,
  877. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  878. .flags = GENL_ADMIN_PERM,
  879. .doit = swconfig_set_attr,
  880. },
  881. {
  882. .cmd = SWITCH_CMD_SET_VLAN,
  883. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  884. .flags = GENL_ADMIN_PERM,
  885. .doit = swconfig_set_attr,
  886. },
  887. {
  888. .cmd = SWITCH_CMD_SET_PORT,
  889. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  890. .flags = GENL_ADMIN_PERM,
  891. .doit = swconfig_set_attr,
  892. },
  893. {
  894. .cmd = SWITCH_CMD_GET_SWITCH,
  895. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  896. .dumpit = swconfig_dump_switches,
  897. .done = swconfig_done,
  898. }
  899. };
  900. static struct genl_family switch_fam = {
  901. .name = "switch",
  902. .hdrsize = 0,
  903. .version = 1,
  904. .maxattr = SWITCH_ATTR_MAX,
  905. .policy = switch_policy,
  906. .module = THIS_MODULE,
  907. .ops = swconfig_ops,
  908. .n_ops = ARRAY_SIZE(swconfig_ops),
  909. #if LINUX_VERSION_CODE > KERNEL_VERSION(6,0,0)
  910. .resv_start_op = SWITCH_CMD_SET_VLAN + 1,
  911. #endif
  912. };
  913. #ifdef CONFIG_OF
  914. void
  915. of_switch_load_portmap(struct switch_dev *dev)
  916. {
  917. struct device_node *port;
  918. if (!dev->of_node)
  919. return;
  920. for_each_child_of_node(dev->of_node, port) {
  921. const __be32 *prop;
  922. const char *segment;
  923. int size, phys;
  924. if (!of_device_is_compatible(port, "swconfig,port"))
  925. continue;
  926. if (of_property_read_string(port, "swconfig,segment", &segment))
  927. continue;
  928. prop = of_get_property(port, "swconfig,portmap", &size);
  929. if (!prop)
  930. continue;
  931. if (size != (2 * sizeof(*prop))) {
  932. pr_err("%s: failed to parse port mapping\n",
  933. port->name);
  934. continue;
  935. }
  936. phys = be32_to_cpup(prop++);
  937. if ((phys < 0) | (phys >= dev->ports)) {
  938. pr_err("%s: physical port index out of range\n",
  939. port->name);
  940. continue;
  941. }
  942. dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
  943. dev->portmap[phys].virt = be32_to_cpup(prop);
  944. pr_debug("Found port: %s, physical: %d, virtual: %d\n",
  945. segment, phys, dev->portmap[phys].virt);
  946. }
  947. }
  948. #endif
  949. int
  950. register_switch(struct switch_dev *dev, struct net_device *netdev)
  951. {
  952. struct switch_dev *sdev;
  953. const int max_switches = 8 * sizeof(unsigned long);
  954. unsigned long in_use = 0;
  955. int err;
  956. int i;
  957. INIT_LIST_HEAD(&dev->dev_list);
  958. if (netdev) {
  959. dev->netdev = netdev;
  960. if (!dev->alias)
  961. dev->alias = netdev->name;
  962. }
  963. BUG_ON(!dev->alias);
  964. /* Make sure swdev_id doesn't overflow */
  965. if (swdev_id == INT_MAX) {
  966. return -ENOMEM;
  967. }
  968. if (dev->ports > 0) {
  969. dev->portbuf = kzalloc(sizeof(struct switch_port) *
  970. dev->ports, GFP_KERNEL);
  971. if (!dev->portbuf)
  972. return -ENOMEM;
  973. dev->portmap = kzalloc(sizeof(struct switch_portmap) *
  974. dev->ports, GFP_KERNEL);
  975. if (!dev->portmap) {
  976. kfree(dev->portbuf);
  977. return -ENOMEM;
  978. }
  979. }
  980. swconfig_defaults_init(dev);
  981. mutex_init(&dev->sw_mutex);
  982. swconfig_lock();
  983. dev->id = ++swdev_id;
  984. list_for_each_entry(sdev, &swdevs, dev_list) {
  985. if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
  986. continue;
  987. if (i < 0 || i > max_switches)
  988. continue;
  989. set_bit(i, &in_use);
  990. }
  991. i = find_first_zero_bit(&in_use, max_switches);
  992. if (i == max_switches) {
  993. swconfig_unlock();
  994. return -ENFILE;
  995. }
  996. #ifdef CONFIG_OF
  997. if (dev->ports)
  998. of_switch_load_portmap(dev);
  999. #endif
  1000. /* fill device name */
  1001. snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
  1002. list_add_tail(&dev->dev_list, &swdevs);
  1003. swconfig_unlock();
  1004. err = swconfig_create_led_trigger(dev);
  1005. if (err)
  1006. return err;
  1007. return 0;
  1008. }
  1009. EXPORT_SYMBOL_GPL(register_switch);
  1010. void
  1011. unregister_switch(struct switch_dev *dev)
  1012. {
  1013. swconfig_destroy_led_trigger(dev);
  1014. kfree(dev->portbuf);
  1015. mutex_lock(&dev->sw_mutex);
  1016. swconfig_lock();
  1017. list_del(&dev->dev_list);
  1018. swconfig_unlock();
  1019. mutex_unlock(&dev->sw_mutex);
  1020. }
  1021. EXPORT_SYMBOL_GPL(unregister_switch);
  1022. int
  1023. switch_generic_set_link(struct switch_dev *dev, int port,
  1024. struct switch_port_link *link)
  1025. {
  1026. if (WARN_ON(!dev->ops->phy_write16))
  1027. return -ENOTSUPP;
  1028. /* Generic implementation */
  1029. if (link->aneg) {
  1030. dev->ops->phy_write16(dev, port, MII_BMCR, 0x0000);
  1031. dev->ops->phy_write16(dev, port, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  1032. } else {
  1033. u16 bmcr = 0;
  1034. if (link->duplex)
  1035. bmcr |= BMCR_FULLDPLX;
  1036. switch (link->speed) {
  1037. case SWITCH_PORT_SPEED_10:
  1038. break;
  1039. case SWITCH_PORT_SPEED_100:
  1040. bmcr |= BMCR_SPEED100;
  1041. break;
  1042. case SWITCH_PORT_SPEED_1000:
  1043. bmcr |= BMCR_SPEED1000;
  1044. break;
  1045. default:
  1046. return -ENOTSUPP;
  1047. }
  1048. dev->ops->phy_write16(dev, port, MII_BMCR, bmcr);
  1049. }
  1050. return 0;
  1051. }
  1052. EXPORT_SYMBOL_GPL(switch_generic_set_link);
  1053. static int __init
  1054. swconfig_init(void)
  1055. {
  1056. INIT_LIST_HEAD(&swdevs);
  1057. return genl_register_family(&switch_fam);
  1058. }
  1059. static void __exit
  1060. swconfig_exit(void)
  1061. {
  1062. genl_unregister_family(&switch_fam);
  1063. }
  1064. module_init(swconfig_init);
  1065. module_exit(swconfig_exit);