swconfig.c 27 KB

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