swconfig.c 24 KB

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