swconfig.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /*
  2. * swconfig.c: Switch configuration API
  3. *
  4. * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  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 <nbd@nbd.name>");
  31. MODULE_LICENSE("GPL");
  32. static int swdev_id;
  33. static struct list_head swdevs;
  34. static DEFINE_SPINLOCK(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. .id = GENL_ID_GENERATE,
  221. .name = "switch",
  222. .hdrsize = 0,
  223. .version = 1,
  224. .maxattr = SWITCH_ATTR_MAX,
  225. };
  226. static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
  227. [SWITCH_ATTR_ID] = { .type = NLA_U32 },
  228. [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
  229. [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
  230. [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
  231. [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
  232. [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
  233. [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
  234. [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
  235. };
  236. static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
  237. [SWITCH_PORT_ID] = { .type = NLA_U32 },
  238. [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
  239. };
  240. static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
  241. [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
  242. [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
  243. [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
  244. };
  245. static inline void
  246. swconfig_lock(void)
  247. {
  248. spin_lock(&swdevs_lock);
  249. }
  250. static inline void
  251. swconfig_unlock(void)
  252. {
  253. spin_unlock(&swdevs_lock);
  254. }
  255. static struct switch_dev *
  256. swconfig_get_dev(struct genl_info *info)
  257. {
  258. struct switch_dev *dev = NULL;
  259. struct switch_dev *p;
  260. int id;
  261. if (!info->attrs[SWITCH_ATTR_ID])
  262. goto done;
  263. id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
  264. swconfig_lock();
  265. list_for_each_entry(p, &swdevs, dev_list) {
  266. if (id != p->id)
  267. continue;
  268. dev = p;
  269. break;
  270. }
  271. if (dev)
  272. mutex_lock(&dev->sw_mutex);
  273. else
  274. pr_debug("device %d not found\n", id);
  275. swconfig_unlock();
  276. done:
  277. return dev;
  278. }
  279. static inline void
  280. swconfig_put_dev(struct switch_dev *dev)
  281. {
  282. mutex_unlock(&dev->sw_mutex);
  283. }
  284. static int
  285. swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
  286. {
  287. struct switch_attr *op = arg;
  288. struct genl_info *info = cb->info;
  289. struct sk_buff *msg = cb->msg;
  290. int id = cb->args[0];
  291. void *hdr;
  292. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
  293. NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
  294. if (IS_ERR(hdr))
  295. return -1;
  296. if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id))
  297. goto nla_put_failure;
  298. if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type))
  299. goto nla_put_failure;
  300. if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name))
  301. goto nla_put_failure;
  302. if (op->description)
  303. if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION,
  304. op->description))
  305. goto nla_put_failure;
  306. genlmsg_end(msg, hdr);
  307. return msg->len;
  308. nla_put_failure:
  309. genlmsg_cancel(msg, hdr);
  310. return -EMSGSIZE;
  311. }
  312. /* spread multipart messages across multiple message buffers */
  313. static int
  314. swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
  315. {
  316. struct genl_info *info = cb->info;
  317. int restart = 0;
  318. int err;
  319. do {
  320. if (!cb->msg) {
  321. cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  322. if (cb->msg == NULL)
  323. goto error;
  324. }
  325. if (!(cb->fill(cb, arg) < 0))
  326. break;
  327. /* fill failed, check if this was already the second attempt */
  328. if (restart)
  329. goto error;
  330. /* try again in a new message, send the current one */
  331. restart = 1;
  332. if (cb->close) {
  333. if (cb->close(cb, arg) < 0)
  334. goto error;
  335. }
  336. err = genlmsg_reply(cb->msg, info);
  337. cb->msg = NULL;
  338. if (err < 0)
  339. goto error;
  340. } while (restart);
  341. return 0;
  342. error:
  343. if (cb->msg)
  344. nlmsg_free(cb->msg);
  345. return -1;
  346. }
  347. static int
  348. swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
  349. {
  350. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  351. const struct switch_attrlist *alist;
  352. struct switch_dev *dev;
  353. struct swconfig_callback cb;
  354. int err = -EINVAL;
  355. int i;
  356. /* defaults */
  357. struct switch_attr *def_list;
  358. unsigned long *def_active;
  359. int n_def;
  360. dev = swconfig_get_dev(info);
  361. if (!dev)
  362. return -EINVAL;
  363. switch (hdr->cmd) {
  364. case SWITCH_CMD_LIST_GLOBAL:
  365. alist = &dev->ops->attr_global;
  366. def_list = default_global;
  367. def_active = &dev->def_global;
  368. n_def = ARRAY_SIZE(default_global);
  369. break;
  370. case SWITCH_CMD_LIST_VLAN:
  371. alist = &dev->ops->attr_vlan;
  372. def_list = default_vlan;
  373. def_active = &dev->def_vlan;
  374. n_def = ARRAY_SIZE(default_vlan);
  375. break;
  376. case SWITCH_CMD_LIST_PORT:
  377. alist = &dev->ops->attr_port;
  378. def_list = default_port;
  379. def_active = &dev->def_port;
  380. n_def = ARRAY_SIZE(default_port);
  381. break;
  382. default:
  383. WARN_ON(1);
  384. goto out;
  385. }
  386. memset(&cb, 0, sizeof(cb));
  387. cb.info = info;
  388. cb.fill = swconfig_dump_attr;
  389. for (i = 0; i < alist->n_attr; i++) {
  390. if (alist->attr[i].disabled)
  391. continue;
  392. cb.args[0] = i;
  393. err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
  394. if (err < 0)
  395. goto error;
  396. }
  397. /* defaults */
  398. for (i = 0; i < n_def; i++) {
  399. if (!test_bit(i, def_active))
  400. continue;
  401. cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
  402. err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
  403. if (err < 0)
  404. goto error;
  405. }
  406. swconfig_put_dev(dev);
  407. if (!cb.msg)
  408. return 0;
  409. return genlmsg_reply(cb.msg, info);
  410. error:
  411. if (cb.msg)
  412. nlmsg_free(cb.msg);
  413. out:
  414. swconfig_put_dev(dev);
  415. return err;
  416. }
  417. static const struct switch_attr *
  418. swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
  419. struct switch_val *val)
  420. {
  421. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  422. const struct switch_attrlist *alist;
  423. const struct switch_attr *attr = NULL;
  424. unsigned int attr_id;
  425. /* defaults */
  426. struct switch_attr *def_list;
  427. unsigned long *def_active;
  428. int n_def;
  429. if (!info->attrs[SWITCH_ATTR_OP_ID])
  430. goto done;
  431. switch (hdr->cmd) {
  432. case SWITCH_CMD_SET_GLOBAL:
  433. case SWITCH_CMD_GET_GLOBAL:
  434. alist = &dev->ops->attr_global;
  435. def_list = default_global;
  436. def_active = &dev->def_global;
  437. n_def = ARRAY_SIZE(default_global);
  438. break;
  439. case SWITCH_CMD_SET_VLAN:
  440. case SWITCH_CMD_GET_VLAN:
  441. alist = &dev->ops->attr_vlan;
  442. def_list = default_vlan;
  443. def_active = &dev->def_vlan;
  444. n_def = ARRAY_SIZE(default_vlan);
  445. if (!info->attrs[SWITCH_ATTR_OP_VLAN])
  446. goto done;
  447. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
  448. if (val->port_vlan >= dev->vlans)
  449. goto done;
  450. break;
  451. case SWITCH_CMD_SET_PORT:
  452. case SWITCH_CMD_GET_PORT:
  453. alist = &dev->ops->attr_port;
  454. def_list = default_port;
  455. def_active = &dev->def_port;
  456. n_def = ARRAY_SIZE(default_port);
  457. if (!info->attrs[SWITCH_ATTR_OP_PORT])
  458. goto done;
  459. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
  460. if (val->port_vlan >= dev->ports)
  461. goto done;
  462. break;
  463. default:
  464. WARN_ON(1);
  465. goto done;
  466. }
  467. if (!alist)
  468. goto done;
  469. attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
  470. if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
  471. attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
  472. if (attr_id >= n_def)
  473. goto done;
  474. if (!test_bit(attr_id, def_active))
  475. goto done;
  476. attr = &def_list[attr_id];
  477. } else {
  478. if (attr_id >= alist->n_attr)
  479. goto done;
  480. attr = &alist->attr[attr_id];
  481. }
  482. if (attr->disabled)
  483. attr = NULL;
  484. done:
  485. if (!attr)
  486. pr_debug("attribute lookup failed\n");
  487. val->attr = attr;
  488. return attr;
  489. }
  490. static int
  491. swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
  492. struct switch_val *val, int max)
  493. {
  494. struct nlattr *nla;
  495. int rem;
  496. val->len = 0;
  497. nla_for_each_nested(nla, head, rem) {
  498. struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
  499. struct switch_port *port;
  500. if (val->len >= max)
  501. return -EINVAL;
  502. port = &val->value.ports[val->len];
  503. if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
  504. port_policy))
  505. return -EINVAL;
  506. if (!tb[SWITCH_PORT_ID])
  507. return -EINVAL;
  508. port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
  509. if (tb[SWITCH_PORT_FLAG_TAGGED])
  510. port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
  511. val->len++;
  512. }
  513. return 0;
  514. }
  515. static int
  516. swconfig_parse_link(struct sk_buff *msg, struct nlattr *nla,
  517. struct switch_port_link *link)
  518. {
  519. struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
  520. if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy))
  521. return -EINVAL;
  522. link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
  523. link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
  524. link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
  525. return 0;
  526. }
  527. static int
  528. swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
  529. {
  530. const struct switch_attr *attr;
  531. struct switch_dev *dev;
  532. struct switch_val val;
  533. int err = -EINVAL;
  534. if (!capable(CAP_NET_ADMIN))
  535. return -EPERM;
  536. dev = swconfig_get_dev(info);
  537. if (!dev)
  538. return -EINVAL;
  539. memset(&val, 0, sizeof(val));
  540. attr = swconfig_lookup_attr(dev, info, &val);
  541. if (!attr || !attr->set)
  542. goto error;
  543. val.attr = attr;
  544. switch (attr->type) {
  545. case SWITCH_TYPE_NOVAL:
  546. break;
  547. case SWITCH_TYPE_INT:
  548. if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
  549. goto error;
  550. val.value.i =
  551. nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
  552. break;
  553. case SWITCH_TYPE_STRING:
  554. if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
  555. goto error;
  556. val.value.s =
  557. nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
  558. break;
  559. case SWITCH_TYPE_PORTS:
  560. val.value.ports = dev->portbuf;
  561. memset(dev->portbuf, 0,
  562. sizeof(struct switch_port) * dev->ports);
  563. /* TODO: implement multipart? */
  564. if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
  565. err = swconfig_parse_ports(skb,
  566. info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
  567. &val, dev->ports);
  568. if (err < 0)
  569. goto error;
  570. } else {
  571. val.len = 0;
  572. err = 0;
  573. }
  574. break;
  575. case SWITCH_TYPE_LINK:
  576. val.value.link = &dev->linkbuf;
  577. memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
  578. if (info->attrs[SWITCH_ATTR_OP_VALUE_LINK]) {
  579. err = swconfig_parse_link(skb,
  580. info->attrs[SWITCH_ATTR_OP_VALUE_LINK],
  581. val.value.link);
  582. if (err < 0)
  583. goto error;
  584. } else {
  585. val.len = 0;
  586. err = 0;
  587. }
  588. break;
  589. default:
  590. goto error;
  591. }
  592. err = attr->set(dev, attr, &val);
  593. error:
  594. swconfig_put_dev(dev);
  595. return err;
  596. }
  597. static int
  598. swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
  599. {
  600. if (cb->nest[0])
  601. nla_nest_end(cb->msg, cb->nest[0]);
  602. return 0;
  603. }
  604. static int
  605. swconfig_send_port(struct swconfig_callback *cb, void *arg)
  606. {
  607. const struct switch_port *port = arg;
  608. struct nlattr *p = NULL;
  609. if (!cb->nest[0]) {
  610. cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
  611. if (!cb->nest[0])
  612. return -1;
  613. }
  614. p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
  615. if (!p)
  616. goto error;
  617. if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id))
  618. goto nla_put_failure;
  619. if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  620. if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED))
  621. goto nla_put_failure;
  622. }
  623. nla_nest_end(cb->msg, p);
  624. return 0;
  625. nla_put_failure:
  626. nla_nest_cancel(cb->msg, p);
  627. error:
  628. nla_nest_cancel(cb->msg, cb->nest[0]);
  629. return -1;
  630. }
  631. static int
  632. swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
  633. const struct switch_val *val)
  634. {
  635. struct swconfig_callback cb;
  636. int err = 0;
  637. int i;
  638. if (!val->value.ports)
  639. return -EINVAL;
  640. memset(&cb, 0, sizeof(cb));
  641. cb.cmd = attr;
  642. cb.msg = *msg;
  643. cb.info = info;
  644. cb.fill = swconfig_send_port;
  645. cb.close = swconfig_close_portlist;
  646. cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
  647. for (i = 0; i < val->len; i++) {
  648. err = swconfig_send_multipart(&cb, &val->value.ports[i]);
  649. if (err)
  650. goto done;
  651. }
  652. err = val->len;
  653. swconfig_close_portlist(&cb, NULL);
  654. *msg = cb.msg;
  655. done:
  656. return err;
  657. }
  658. static int
  659. swconfig_send_link(struct sk_buff *msg, struct genl_info *info, int attr,
  660. const struct switch_port_link *link)
  661. {
  662. struct nlattr *p = NULL;
  663. int err = 0;
  664. p = nla_nest_start(msg, attr);
  665. if (link->link) {
  666. if (nla_put_flag(msg, SWITCH_LINK_FLAG_LINK))
  667. goto nla_put_failure;
  668. }
  669. if (link->duplex) {
  670. if (nla_put_flag(msg, SWITCH_LINK_FLAG_DUPLEX))
  671. goto nla_put_failure;
  672. }
  673. if (link->aneg) {
  674. if (nla_put_flag(msg, SWITCH_LINK_FLAG_ANEG))
  675. goto nla_put_failure;
  676. }
  677. if (link->tx_flow) {
  678. if (nla_put_flag(msg, SWITCH_LINK_FLAG_TX_FLOW))
  679. goto nla_put_failure;
  680. }
  681. if (link->rx_flow) {
  682. if (nla_put_flag(msg, SWITCH_LINK_FLAG_RX_FLOW))
  683. goto nla_put_failure;
  684. }
  685. if (nla_put_u32(msg, SWITCH_LINK_SPEED, link->speed))
  686. goto nla_put_failure;
  687. if (link->eee & ADVERTISED_100baseT_Full) {
  688. if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_100BASET))
  689. goto nla_put_failure;
  690. }
  691. if (link->eee & ADVERTISED_1000baseT_Full) {
  692. if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_1000BASET))
  693. goto nla_put_failure;
  694. }
  695. nla_nest_end(msg, p);
  696. return err;
  697. nla_put_failure:
  698. nla_nest_cancel(msg, p);
  699. return -1;
  700. }
  701. static int
  702. swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
  703. {
  704. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  705. const struct switch_attr *attr;
  706. struct switch_dev *dev;
  707. struct sk_buff *msg = NULL;
  708. struct switch_val val;
  709. int err = -EINVAL;
  710. int cmd = hdr->cmd;
  711. dev = swconfig_get_dev(info);
  712. if (!dev)
  713. return -EINVAL;
  714. memset(&val, 0, sizeof(val));
  715. attr = swconfig_lookup_attr(dev, info, &val);
  716. if (!attr || !attr->get)
  717. goto error;
  718. if (attr->type == SWITCH_TYPE_PORTS) {
  719. val.value.ports = dev->portbuf;
  720. memset(dev->portbuf, 0,
  721. sizeof(struct switch_port) * dev->ports);
  722. } else if (attr->type == SWITCH_TYPE_LINK) {
  723. val.value.link = &dev->linkbuf;
  724. memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
  725. }
  726. err = attr->get(dev, attr, &val);
  727. if (err)
  728. goto error;
  729. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  730. if (!msg)
  731. goto error;
  732. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
  733. 0, cmd);
  734. if (IS_ERR(hdr))
  735. goto nla_put_failure;
  736. switch (attr->type) {
  737. case SWITCH_TYPE_INT:
  738. if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
  739. goto nla_put_failure;
  740. break;
  741. case SWITCH_TYPE_STRING:
  742. if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s))
  743. goto nla_put_failure;
  744. break;
  745. case SWITCH_TYPE_PORTS:
  746. err = swconfig_send_ports(&msg, info,
  747. SWITCH_ATTR_OP_VALUE_PORTS, &val);
  748. if (err < 0)
  749. goto nla_put_failure;
  750. break;
  751. case SWITCH_TYPE_LINK:
  752. err = swconfig_send_link(msg, info,
  753. SWITCH_ATTR_OP_VALUE_LINK, val.value.link);
  754. if (err < 0)
  755. goto nla_put_failure;
  756. break;
  757. default:
  758. pr_debug("invalid type in attribute\n");
  759. err = -EINVAL;
  760. goto error;
  761. }
  762. genlmsg_end(msg, hdr);
  763. err = msg->len;
  764. if (err < 0)
  765. goto nla_put_failure;
  766. swconfig_put_dev(dev);
  767. return genlmsg_reply(msg, info);
  768. nla_put_failure:
  769. if (msg)
  770. nlmsg_free(msg);
  771. error:
  772. swconfig_put_dev(dev);
  773. if (!err)
  774. err = -ENOMEM;
  775. return err;
  776. }
  777. static int
  778. swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
  779. const struct switch_dev *dev)
  780. {
  781. struct nlattr *p = NULL, *m = NULL;
  782. void *hdr;
  783. int i;
  784. hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
  785. SWITCH_CMD_NEW_ATTR);
  786. if (IS_ERR(hdr))
  787. return -1;
  788. if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id))
  789. goto nla_put_failure;
  790. if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname))
  791. goto nla_put_failure;
  792. if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias))
  793. goto nla_put_failure;
  794. if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name))
  795. goto nla_put_failure;
  796. if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans))
  797. goto nla_put_failure;
  798. if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports))
  799. goto nla_put_failure;
  800. if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
  801. goto nla_put_failure;
  802. m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
  803. if (!m)
  804. goto nla_put_failure;
  805. for (i = 0; i < dev->ports; i++) {
  806. p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
  807. if (!p)
  808. continue;
  809. if (dev->portmap[i].s) {
  810. if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
  811. dev->portmap[i].s))
  812. goto nla_put_failure;
  813. if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
  814. dev->portmap[i].virt))
  815. goto nla_put_failure;
  816. }
  817. nla_nest_end(msg, p);
  818. }
  819. nla_nest_end(msg, m);
  820. genlmsg_end(msg, hdr);
  821. return msg->len;
  822. nla_put_failure:
  823. genlmsg_cancel(msg, hdr);
  824. return -EMSGSIZE;
  825. }
  826. static int swconfig_dump_switches(struct sk_buff *skb,
  827. struct netlink_callback *cb)
  828. {
  829. struct switch_dev *dev;
  830. int start = cb->args[0];
  831. int idx = 0;
  832. swconfig_lock();
  833. list_for_each_entry(dev, &swdevs, dev_list) {
  834. if (++idx <= start)
  835. continue;
  836. if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid,
  837. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  838. dev) < 0)
  839. break;
  840. }
  841. swconfig_unlock();
  842. cb->args[0] = idx;
  843. return skb->len;
  844. }
  845. static int
  846. swconfig_done(struct netlink_callback *cb)
  847. {
  848. return 0;
  849. }
  850. static struct genl_ops swconfig_ops[] = {
  851. {
  852. .cmd = SWITCH_CMD_LIST_GLOBAL,
  853. .doit = swconfig_list_attrs,
  854. .policy = switch_policy,
  855. },
  856. {
  857. .cmd = SWITCH_CMD_LIST_VLAN,
  858. .doit = swconfig_list_attrs,
  859. .policy = switch_policy,
  860. },
  861. {
  862. .cmd = SWITCH_CMD_LIST_PORT,
  863. .doit = swconfig_list_attrs,
  864. .policy = switch_policy,
  865. },
  866. {
  867. .cmd = SWITCH_CMD_GET_GLOBAL,
  868. .doit = swconfig_get_attr,
  869. .policy = switch_policy,
  870. },
  871. {
  872. .cmd = SWITCH_CMD_GET_VLAN,
  873. .doit = swconfig_get_attr,
  874. .policy = switch_policy,
  875. },
  876. {
  877. .cmd = SWITCH_CMD_GET_PORT,
  878. .doit = swconfig_get_attr,
  879. .policy = switch_policy,
  880. },
  881. {
  882. .cmd = SWITCH_CMD_SET_GLOBAL,
  883. .flags = GENL_ADMIN_PERM,
  884. .doit = swconfig_set_attr,
  885. .policy = switch_policy,
  886. },
  887. {
  888. .cmd = SWITCH_CMD_SET_VLAN,
  889. .flags = GENL_ADMIN_PERM,
  890. .doit = swconfig_set_attr,
  891. .policy = switch_policy,
  892. },
  893. {
  894. .cmd = SWITCH_CMD_SET_PORT,
  895. .flags = GENL_ADMIN_PERM,
  896. .doit = swconfig_set_attr,
  897. .policy = switch_policy,
  898. },
  899. {
  900. .cmd = SWITCH_CMD_GET_SWITCH,
  901. .dumpit = swconfig_dump_switches,
  902. .policy = switch_policy,
  903. .done = swconfig_done,
  904. }
  905. };
  906. #ifdef CONFIG_OF
  907. void
  908. of_switch_load_portmap(struct switch_dev *dev)
  909. {
  910. struct device_node *port;
  911. if (!dev->of_node)
  912. return;
  913. for_each_child_of_node(dev->of_node, port) {
  914. const __be32 *prop;
  915. const char *segment;
  916. int size, phys;
  917. if (!of_device_is_compatible(port, "swconfig,port"))
  918. continue;
  919. if (of_property_read_string(port, "swconfig,segment", &segment))
  920. continue;
  921. prop = of_get_property(port, "swconfig,portmap", &size);
  922. if (!prop)
  923. continue;
  924. if (size != (2 * sizeof(*prop))) {
  925. pr_err("%s: failed to parse port mapping\n",
  926. port->name);
  927. continue;
  928. }
  929. phys = be32_to_cpup(prop++);
  930. if ((phys < 0) | (phys >= dev->ports)) {
  931. pr_err("%s: physical port index out of range\n",
  932. port->name);
  933. continue;
  934. }
  935. dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
  936. dev->portmap[phys].virt = be32_to_cpup(prop);
  937. pr_debug("Found port: %s, physical: %d, virtual: %d\n",
  938. segment, phys, dev->portmap[phys].virt);
  939. }
  940. }
  941. #endif
  942. int
  943. register_switch(struct switch_dev *dev, struct net_device *netdev)
  944. {
  945. struct switch_dev *sdev;
  946. const int max_switches = 8 * sizeof(unsigned long);
  947. unsigned long in_use = 0;
  948. int err;
  949. int i;
  950. INIT_LIST_HEAD(&dev->dev_list);
  951. if (netdev) {
  952. dev->netdev = netdev;
  953. if (!dev->alias)
  954. dev->alias = netdev->name;
  955. }
  956. BUG_ON(!dev->alias);
  957. /* Make sure swdev_id doesn't overflow */
  958. if (swdev_id == INT_MAX) {
  959. return -ENOMEM;
  960. }
  961. if (dev->ports > 0) {
  962. dev->portbuf = kzalloc(sizeof(struct switch_port) *
  963. dev->ports, GFP_KERNEL);
  964. if (!dev->portbuf)
  965. return -ENOMEM;
  966. dev->portmap = kzalloc(sizeof(struct switch_portmap) *
  967. dev->ports, GFP_KERNEL);
  968. if (!dev->portmap) {
  969. kfree(dev->portbuf);
  970. return -ENOMEM;
  971. }
  972. }
  973. swconfig_defaults_init(dev);
  974. mutex_init(&dev->sw_mutex);
  975. swconfig_lock();
  976. dev->id = ++swdev_id;
  977. list_for_each_entry(sdev, &swdevs, dev_list) {
  978. if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
  979. continue;
  980. if (i < 0 || i > max_switches)
  981. continue;
  982. set_bit(i, &in_use);
  983. }
  984. i = find_first_zero_bit(&in_use, max_switches);
  985. if (i == max_switches) {
  986. swconfig_unlock();
  987. return -ENFILE;
  988. }
  989. #ifdef CONFIG_OF
  990. if (dev->ports)
  991. of_switch_load_portmap(dev);
  992. #endif
  993. /* fill device name */
  994. snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
  995. list_add_tail(&dev->dev_list, &swdevs);
  996. swconfig_unlock();
  997. err = swconfig_create_led_trigger(dev);
  998. if (err)
  999. return err;
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL_GPL(register_switch);
  1003. void
  1004. unregister_switch(struct switch_dev *dev)
  1005. {
  1006. swconfig_destroy_led_trigger(dev);
  1007. kfree(dev->portbuf);
  1008. mutex_lock(&dev->sw_mutex);
  1009. swconfig_lock();
  1010. list_del(&dev->dev_list);
  1011. swconfig_unlock();
  1012. mutex_unlock(&dev->sw_mutex);
  1013. }
  1014. EXPORT_SYMBOL_GPL(unregister_switch);
  1015. int
  1016. switch_generic_set_link(struct switch_dev *dev, int port,
  1017. struct switch_port_link *link)
  1018. {
  1019. if (WARN_ON(!dev->ops->phy_write16))
  1020. return -ENOTSUPP;
  1021. /* Generic implementation */
  1022. if (link->aneg) {
  1023. dev->ops->phy_write16(dev, port, MII_BMCR, 0x0000);
  1024. dev->ops->phy_write16(dev, port, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  1025. } else {
  1026. u16 bmcr = 0;
  1027. if (link->duplex)
  1028. bmcr |= BMCR_FULLDPLX;
  1029. switch (link->speed) {
  1030. case SWITCH_PORT_SPEED_10:
  1031. break;
  1032. case SWITCH_PORT_SPEED_100:
  1033. bmcr |= BMCR_SPEED100;
  1034. break;
  1035. case SWITCH_PORT_SPEED_1000:
  1036. bmcr |= BMCR_SPEED1000;
  1037. break;
  1038. default:
  1039. return -ENOTSUPP;
  1040. }
  1041. dev->ops->phy_write16(dev, port, MII_BMCR, bmcr);
  1042. }
  1043. return 0;
  1044. }
  1045. static int __init
  1046. swconfig_init(void)
  1047. {
  1048. INIT_LIST_HEAD(&swdevs);
  1049. return genl_register_family_with_ops(&switch_fam, swconfig_ops);
  1050. }
  1051. static void __exit
  1052. swconfig_exit(void)
  1053. {
  1054. genl_unregister_family(&switch_fam);
  1055. }
  1056. module_init(swconfig_init);
  1057. module_exit(swconfig_exit);