swconfig.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. //#define DEBUG 1
  26. #ifdef DEBUG
  27. #define DPRINTF(format, ...) printk("%s: " format, __func__, ##__VA_ARGS__)
  28. #else
  29. #define DPRINTF(...) do {} while(0)
  30. #endif
  31. #define SWCONFIG_DEVNAME "switch%d"
  32. #include "swconfig_leds.c"
  33. MODULE_AUTHOR("Felix Fietkau <[email protected]>");
  34. MODULE_LICENSE("GPL");
  35. static int swdev_id = 0;
  36. static struct list_head swdevs;
  37. static DEFINE_SPINLOCK(swdevs_lock);
  38. struct swconfig_callback;
  39. struct swconfig_callback
  40. {
  41. struct sk_buff *msg;
  42. struct genlmsghdr *hdr;
  43. struct genl_info *info;
  44. int cmd;
  45. /* callback for filling in the message data */
  46. int (*fill)(struct swconfig_callback *cb, void *arg);
  47. /* callback for closing the message before sending it */
  48. int (*close)(struct swconfig_callback *cb, void *arg);
  49. struct nlattr *nest[4];
  50. int args[4];
  51. };
  52. /* defaults */
  53. static int
  54. swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  55. {
  56. int ret;
  57. if (val->port_vlan >= dev->vlans)
  58. return -EINVAL;
  59. if (!dev->ops->get_vlan_ports)
  60. return -EOPNOTSUPP;
  61. ret = dev->ops->get_vlan_ports(dev, val);
  62. return ret;
  63. }
  64. static int
  65. swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  66. {
  67. struct switch_port *ports = val->value.ports;
  68. const struct switch_dev_ops *ops = dev->ops;
  69. int i;
  70. if (val->port_vlan >= dev->vlans)
  71. return -EINVAL;
  72. /* validate ports */
  73. if (val->len > dev->ports)
  74. return -EINVAL;
  75. if (!ops->set_vlan_ports)
  76. return -EOPNOTSUPP;
  77. for (i = 0; i < val->len; i++) {
  78. if (ports[i].id >= dev->ports)
  79. return -EINVAL;
  80. if (ops->set_port_pvid &&
  81. !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
  82. ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
  83. }
  84. return ops->set_vlan_ports(dev, val);
  85. }
  86. static int
  87. swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  88. {
  89. if (val->port_vlan >= dev->ports)
  90. return -EINVAL;
  91. if (!dev->ops->set_port_pvid)
  92. return -EOPNOTSUPP;
  93. return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
  94. }
  95. static int
  96. swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, 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 const char *
  105. swconfig_speed_str(enum switch_port_speed speed)
  106. {
  107. switch (speed) {
  108. case SWITCH_PORT_SPEED_10:
  109. return "10baseT";
  110. case SWITCH_PORT_SPEED_100:
  111. return "100baseT";
  112. case SWITCH_PORT_SPEED_1000:
  113. return "1000baseT";
  114. default:
  115. break;
  116. }
  117. return "unknown";
  118. }
  119. static int
  120. swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr, 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",
  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.aneg ? "auto" : "");
  143. else
  144. len = snprintf(dev->buf, sizeof(dev->buf), "port:%d link:down",
  145. val->port_vlan);
  146. val->value.s = dev->buf;
  147. val->len = len;
  148. return 0;
  149. }
  150. static int
  151. swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  152. {
  153. /* don't complain if not supported by the switch driver */
  154. if (!dev->ops->apply_config)
  155. return 0;
  156. return dev->ops->apply_config(dev);
  157. }
  158. static int
  159. swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  160. {
  161. /* don't complain if not supported by the switch driver */
  162. if (!dev->ops->reset_switch)
  163. return 0;
  164. return dev->ops->reset_switch(dev);
  165. }
  166. enum global_defaults {
  167. GLOBAL_APPLY,
  168. GLOBAL_RESET,
  169. };
  170. enum vlan_defaults {
  171. VLAN_PORTS,
  172. };
  173. enum port_defaults {
  174. PORT_PVID,
  175. PORT_LINK,
  176. };
  177. static struct switch_attr default_global[] = {
  178. [GLOBAL_APPLY] = {
  179. .type = SWITCH_TYPE_NOVAL,
  180. .name = "apply",
  181. .description = "Activate changes in the hardware",
  182. .set = swconfig_apply_config,
  183. },
  184. [GLOBAL_RESET] = {
  185. .type = SWITCH_TYPE_NOVAL,
  186. .name = "reset",
  187. .description = "Reset the switch",
  188. .set = swconfig_reset_switch,
  189. }
  190. };
  191. static struct switch_attr default_port[] = {
  192. [PORT_PVID] = {
  193. .type = SWITCH_TYPE_INT,
  194. .name = "pvid",
  195. .description = "Primary VLAN ID",
  196. .set = swconfig_set_pvid,
  197. .get = swconfig_get_pvid,
  198. },
  199. [PORT_LINK] = {
  200. .type = SWITCH_TYPE_STRING,
  201. .name = "link",
  202. .description = "Get port link information",
  203. .set = NULL,
  204. .get = swconfig_get_link,
  205. }
  206. };
  207. static struct switch_attr default_vlan[] = {
  208. [VLAN_PORTS] = {
  209. .type = SWITCH_TYPE_PORTS,
  210. .name = "ports",
  211. .description = "VLAN port mapping",
  212. .set = swconfig_set_vlan_ports,
  213. .get = swconfig_get_vlan_ports,
  214. },
  215. };
  216. static const struct switch_attr *
  217. swconfig_find_attr_by_name(const struct switch_attrlist *alist, const char *name)
  218. {
  219. int i;
  220. for (i = 0; i < alist->n_attr; i++)
  221. if (strcmp(name, alist->attr[i].name) == 0)
  222. return &alist->attr[i];
  223. return NULL;
  224. }
  225. static void swconfig_defaults_init(struct switch_dev *dev)
  226. {
  227. const struct switch_dev_ops *ops = dev->ops;
  228. dev->def_global = 0;
  229. dev->def_vlan = 0;
  230. dev->def_port = 0;
  231. if (ops->get_vlan_ports || ops->set_vlan_ports)
  232. set_bit(VLAN_PORTS, &dev->def_vlan);
  233. if (ops->get_port_pvid || ops->set_port_pvid)
  234. set_bit(PORT_PVID, &dev->def_port);
  235. if (ops->get_port_link &&
  236. !swconfig_find_attr_by_name(&ops->attr_port, "link"))
  237. set_bit(PORT_LINK, &dev->def_port);
  238. /* always present, can be no-op */
  239. set_bit(GLOBAL_APPLY, &dev->def_global);
  240. set_bit(GLOBAL_RESET, &dev->def_global);
  241. }
  242. static struct genl_family switch_fam = {
  243. .id = GENL_ID_GENERATE,
  244. .name = "switch",
  245. .hdrsize = 0,
  246. .version = 1,
  247. .maxattr = SWITCH_ATTR_MAX,
  248. };
  249. static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
  250. [SWITCH_ATTR_ID] = { .type = NLA_U32 },
  251. [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
  252. [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
  253. [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
  254. [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
  255. [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
  256. [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
  257. [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
  258. };
  259. static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
  260. [SWITCH_PORT_ID] = { .type = NLA_U32 },
  261. [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
  262. };
  263. static inline void
  264. swconfig_lock(void)
  265. {
  266. spin_lock(&swdevs_lock);
  267. }
  268. static inline void
  269. swconfig_unlock(void)
  270. {
  271. spin_unlock(&swdevs_lock);
  272. }
  273. static struct switch_dev *
  274. swconfig_get_dev(struct genl_info *info)
  275. {
  276. struct switch_dev *dev = NULL;
  277. struct switch_dev *p;
  278. int id;
  279. if (!info->attrs[SWITCH_ATTR_ID])
  280. goto done;
  281. id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
  282. swconfig_lock();
  283. list_for_each_entry(p, &swdevs, dev_list) {
  284. if (id != p->id)
  285. continue;
  286. dev = p;
  287. break;
  288. }
  289. if (dev)
  290. mutex_lock(&dev->sw_mutex);
  291. else
  292. DPRINTF("device %d not found\n", id);
  293. swconfig_unlock();
  294. done:
  295. return dev;
  296. }
  297. static inline void
  298. swconfig_put_dev(struct switch_dev *dev)
  299. {
  300. mutex_unlock(&dev->sw_mutex);
  301. }
  302. static int
  303. swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
  304. {
  305. struct switch_attr *op = arg;
  306. struct genl_info *info = cb->info;
  307. struct sk_buff *msg = cb->msg;
  308. int id = cb->args[0];
  309. void *hdr;
  310. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
  311. NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
  312. if (IS_ERR(hdr))
  313. return -1;
  314. NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
  315. NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
  316. NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
  317. if (op->description)
  318. NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
  319. op->description);
  320. return genlmsg_end(msg, hdr);
  321. nla_put_failure:
  322. genlmsg_cancel(msg, hdr);
  323. return -EMSGSIZE;
  324. }
  325. /* spread multipart messages across multiple message buffers */
  326. static int
  327. swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
  328. {
  329. struct genl_info *info = cb->info;
  330. int restart = 0;
  331. int err;
  332. do {
  333. if (!cb->msg) {
  334. cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  335. if (cb->msg == NULL)
  336. goto error;
  337. }
  338. if (!(cb->fill(cb, arg) < 0))
  339. break;
  340. /* fill failed, check if this was already the second attempt */
  341. if (restart)
  342. goto error;
  343. /* try again in a new message, send the current one */
  344. restart = 1;
  345. if (cb->close) {
  346. if (cb->close(cb, arg) < 0)
  347. goto error;
  348. }
  349. err = genlmsg_reply(cb->msg, info);
  350. cb->msg = NULL;
  351. if (err < 0)
  352. goto error;
  353. } while (restart);
  354. return 0;
  355. error:
  356. if (cb->msg)
  357. nlmsg_free(cb->msg);
  358. return -1;
  359. }
  360. static int
  361. swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
  362. {
  363. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  364. const struct switch_attrlist *alist;
  365. struct switch_dev *dev;
  366. struct swconfig_callback cb;
  367. int err = -EINVAL;
  368. int i;
  369. /* defaults */
  370. struct switch_attr *def_list;
  371. unsigned long *def_active;
  372. int n_def;
  373. dev = swconfig_get_dev(info);
  374. if (!dev)
  375. return -EINVAL;
  376. switch(hdr->cmd) {
  377. case SWITCH_CMD_LIST_GLOBAL:
  378. alist = &dev->ops->attr_global;
  379. def_list = default_global;
  380. def_active = &dev->def_global;
  381. n_def = ARRAY_SIZE(default_global);
  382. break;
  383. case SWITCH_CMD_LIST_VLAN:
  384. alist = &dev->ops->attr_vlan;
  385. def_list = default_vlan;
  386. def_active = &dev->def_vlan;
  387. n_def = ARRAY_SIZE(default_vlan);
  388. break;
  389. case SWITCH_CMD_LIST_PORT:
  390. alist = &dev->ops->attr_port;
  391. def_list = default_port;
  392. def_active = &dev->def_port;
  393. n_def = ARRAY_SIZE(default_port);
  394. break;
  395. default:
  396. WARN_ON(1);
  397. goto out;
  398. }
  399. memset(&cb, 0, sizeof(cb));
  400. cb.info = info;
  401. cb.fill = swconfig_dump_attr;
  402. for (i = 0; i < alist->n_attr; i++) {
  403. if (alist->attr[i].disabled)
  404. continue;
  405. cb.args[0] = i;
  406. err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
  407. if (err < 0)
  408. goto error;
  409. }
  410. /* defaults */
  411. for (i = 0; i < n_def; i++) {
  412. if (!test_bit(i, def_active))
  413. continue;
  414. cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
  415. err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
  416. if (err < 0)
  417. goto error;
  418. }
  419. swconfig_put_dev(dev);
  420. if (!cb.msg)
  421. return 0;
  422. return genlmsg_reply(cb.msg, info);
  423. error:
  424. if (cb.msg)
  425. nlmsg_free(cb.msg);
  426. out:
  427. swconfig_put_dev(dev);
  428. return err;
  429. }
  430. static const struct switch_attr *
  431. swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
  432. struct switch_val *val)
  433. {
  434. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  435. const struct switch_attrlist *alist;
  436. const struct switch_attr *attr = NULL;
  437. int attr_id;
  438. /* defaults */
  439. struct switch_attr *def_list;
  440. unsigned long *def_active;
  441. int n_def;
  442. if (!info->attrs[SWITCH_ATTR_OP_ID])
  443. goto done;
  444. switch(hdr->cmd) {
  445. case SWITCH_CMD_SET_GLOBAL:
  446. case SWITCH_CMD_GET_GLOBAL:
  447. alist = &dev->ops->attr_global;
  448. def_list = default_global;
  449. def_active = &dev->def_global;
  450. n_def = ARRAY_SIZE(default_global);
  451. break;
  452. case SWITCH_CMD_SET_VLAN:
  453. case SWITCH_CMD_GET_VLAN:
  454. alist = &dev->ops->attr_vlan;
  455. def_list = default_vlan;
  456. def_active = &dev->def_vlan;
  457. n_def = ARRAY_SIZE(default_vlan);
  458. if (!info->attrs[SWITCH_ATTR_OP_VLAN])
  459. goto done;
  460. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
  461. if (val->port_vlan >= dev->vlans)
  462. goto done;
  463. break;
  464. case SWITCH_CMD_SET_PORT:
  465. case SWITCH_CMD_GET_PORT:
  466. alist = &dev->ops->attr_port;
  467. def_list = default_port;
  468. def_active = &dev->def_port;
  469. n_def = ARRAY_SIZE(default_port);
  470. if (!info->attrs[SWITCH_ATTR_OP_PORT])
  471. goto done;
  472. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
  473. if (val->port_vlan >= dev->ports)
  474. goto done;
  475. break;
  476. default:
  477. WARN_ON(1);
  478. goto done;
  479. }
  480. if (!alist)
  481. goto done;
  482. attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
  483. if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
  484. attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
  485. if (attr_id >= n_def)
  486. goto done;
  487. if (!test_bit(attr_id, def_active))
  488. goto done;
  489. attr = &def_list[attr_id];
  490. } else {
  491. if (attr_id >= alist->n_attr)
  492. goto done;
  493. attr = &alist->attr[attr_id];
  494. }
  495. if (attr->disabled)
  496. attr = NULL;
  497. done:
  498. if (!attr)
  499. DPRINTF("attribute lookup failed\n");
  500. val->attr = attr;
  501. return attr;
  502. }
  503. static int
  504. swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
  505. struct switch_val *val, int max)
  506. {
  507. struct nlattr *nla;
  508. int rem;
  509. val->len = 0;
  510. nla_for_each_nested(nla, head, rem) {
  511. struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
  512. struct switch_port *port = &val->value.ports[val->len];
  513. if (val->len >= max)
  514. return -EINVAL;
  515. if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
  516. port_policy))
  517. return -EINVAL;
  518. if (!tb[SWITCH_PORT_ID])
  519. return -EINVAL;
  520. port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
  521. if (tb[SWITCH_PORT_FLAG_TAGGED])
  522. port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
  523. val->len++;
  524. }
  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. dev = swconfig_get_dev(info);
  535. if (!dev)
  536. return -EINVAL;
  537. memset(&val, 0, sizeof(val));
  538. attr = swconfig_lookup_attr(dev, info, &val);
  539. if (!attr || !attr->set)
  540. goto error;
  541. val.attr = attr;
  542. switch(attr->type) {
  543. case SWITCH_TYPE_NOVAL:
  544. break;
  545. case SWITCH_TYPE_INT:
  546. if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
  547. goto error;
  548. val.value.i =
  549. nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
  550. break;
  551. case SWITCH_TYPE_STRING:
  552. if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
  553. goto error;
  554. val.value.s =
  555. nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
  556. break;
  557. case SWITCH_TYPE_PORTS:
  558. val.value.ports = dev->portbuf;
  559. memset(dev->portbuf, 0,
  560. sizeof(struct switch_port) * dev->ports);
  561. /* TODO: implement multipart? */
  562. if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
  563. err = swconfig_parse_ports(skb,
  564. info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
  565. if (err < 0)
  566. goto error;
  567. } else {
  568. val.len = 0;
  569. err = 0;
  570. }
  571. break;
  572. default:
  573. goto error;
  574. }
  575. err = attr->set(dev, attr, &val);
  576. error:
  577. swconfig_put_dev(dev);
  578. return err;
  579. }
  580. static int
  581. swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
  582. {
  583. if (cb->nest[0])
  584. nla_nest_end(cb->msg, cb->nest[0]);
  585. return 0;
  586. }
  587. static int
  588. swconfig_send_port(struct swconfig_callback *cb, void *arg)
  589. {
  590. const struct switch_port *port = arg;
  591. struct nlattr *p = NULL;
  592. if (!cb->nest[0]) {
  593. cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
  594. if (!cb->nest[0])
  595. return -1;
  596. }
  597. p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
  598. if (!p)
  599. goto error;
  600. NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
  601. if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
  602. NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
  603. nla_nest_end(cb->msg, p);
  604. return 0;
  605. nla_put_failure:
  606. nla_nest_cancel(cb->msg, p);
  607. error:
  608. nla_nest_cancel(cb->msg, cb->nest[0]);
  609. return -1;
  610. }
  611. static int
  612. swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
  613. const struct switch_val *val)
  614. {
  615. struct swconfig_callback cb;
  616. int err = 0;
  617. int i;
  618. if (!val->value.ports)
  619. return -EINVAL;
  620. memset(&cb, 0, sizeof(cb));
  621. cb.cmd = attr;
  622. cb.msg = *msg;
  623. cb.info = info;
  624. cb.fill = swconfig_send_port;
  625. cb.close = swconfig_close_portlist;
  626. cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
  627. for (i = 0; i < val->len; i++) {
  628. err = swconfig_send_multipart(&cb, &val->value.ports[i]);
  629. if (err)
  630. goto done;
  631. }
  632. err = val->len;
  633. swconfig_close_portlist(&cb, NULL);
  634. *msg = cb.msg;
  635. done:
  636. return err;
  637. }
  638. static int
  639. swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
  640. {
  641. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  642. const struct switch_attr *attr;
  643. struct switch_dev *dev;
  644. struct sk_buff *msg = NULL;
  645. struct switch_val val;
  646. int err = -EINVAL;
  647. int cmd = hdr->cmd;
  648. dev = swconfig_get_dev(info);
  649. if (!dev)
  650. return -EINVAL;
  651. memset(&val, 0, sizeof(val));
  652. attr = swconfig_lookup_attr(dev, info, &val);
  653. if (!attr || !attr->get)
  654. goto error;
  655. if (attr->type == SWITCH_TYPE_PORTS) {
  656. val.value.ports = dev->portbuf;
  657. memset(dev->portbuf, 0,
  658. sizeof(struct switch_port) * dev->ports);
  659. }
  660. err = attr->get(dev, attr, &val);
  661. if (err)
  662. goto error;
  663. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  664. if (!msg)
  665. goto error;
  666. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
  667. 0, cmd);
  668. if (IS_ERR(hdr))
  669. goto nla_put_failure;
  670. switch(attr->type) {
  671. case SWITCH_TYPE_INT:
  672. NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
  673. break;
  674. case SWITCH_TYPE_STRING:
  675. NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
  676. break;
  677. case SWITCH_TYPE_PORTS:
  678. err = swconfig_send_ports(&msg, info,
  679. SWITCH_ATTR_OP_VALUE_PORTS, &val);
  680. if (err < 0)
  681. goto nla_put_failure;
  682. break;
  683. default:
  684. DPRINTF("invalid type in attribute\n");
  685. err = -EINVAL;
  686. goto error;
  687. }
  688. err = genlmsg_end(msg, hdr);
  689. if (err < 0)
  690. goto nla_put_failure;
  691. swconfig_put_dev(dev);
  692. return genlmsg_reply(msg, info);
  693. nla_put_failure:
  694. if (msg)
  695. nlmsg_free(msg);
  696. error:
  697. swconfig_put_dev(dev);
  698. if (!err)
  699. err = -ENOMEM;
  700. return err;
  701. }
  702. static int
  703. swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
  704. const struct switch_dev *dev)
  705. {
  706. void *hdr;
  707. hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
  708. SWITCH_CMD_NEW_ATTR);
  709. if (IS_ERR(hdr))
  710. return -1;
  711. NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
  712. NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
  713. NLA_PUT_STRING(msg, SWITCH_ATTR_ALIAS, dev->alias);
  714. NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
  715. NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
  716. NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
  717. NLA_PUT_U32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port);
  718. return genlmsg_end(msg, hdr);
  719. nla_put_failure:
  720. genlmsg_cancel(msg, hdr);
  721. return -EMSGSIZE;
  722. }
  723. static int swconfig_dump_switches(struct sk_buff *skb,
  724. struct netlink_callback *cb)
  725. {
  726. struct switch_dev *dev;
  727. int start = cb->args[0];
  728. int idx = 0;
  729. swconfig_lock();
  730. list_for_each_entry(dev, &swdevs, dev_list) {
  731. if (++idx <= start)
  732. continue;
  733. if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
  734. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  735. dev) < 0)
  736. break;
  737. }
  738. swconfig_unlock();
  739. cb->args[0] = idx;
  740. return skb->len;
  741. }
  742. static int
  743. swconfig_done(struct netlink_callback *cb)
  744. {
  745. return 0;
  746. }
  747. static struct genl_ops swconfig_ops[] = {
  748. {
  749. .cmd = SWITCH_CMD_LIST_GLOBAL,
  750. .doit = swconfig_list_attrs,
  751. .policy = switch_policy,
  752. },
  753. {
  754. .cmd = SWITCH_CMD_LIST_VLAN,
  755. .doit = swconfig_list_attrs,
  756. .policy = switch_policy,
  757. },
  758. {
  759. .cmd = SWITCH_CMD_LIST_PORT,
  760. .doit = swconfig_list_attrs,
  761. .policy = switch_policy,
  762. },
  763. {
  764. .cmd = SWITCH_CMD_GET_GLOBAL,
  765. .doit = swconfig_get_attr,
  766. .policy = switch_policy,
  767. },
  768. {
  769. .cmd = SWITCH_CMD_GET_VLAN,
  770. .doit = swconfig_get_attr,
  771. .policy = switch_policy,
  772. },
  773. {
  774. .cmd = SWITCH_CMD_GET_PORT,
  775. .doit = swconfig_get_attr,
  776. .policy = switch_policy,
  777. },
  778. {
  779. .cmd = SWITCH_CMD_SET_GLOBAL,
  780. .doit = swconfig_set_attr,
  781. .policy = switch_policy,
  782. },
  783. {
  784. .cmd = SWITCH_CMD_SET_VLAN,
  785. .doit = swconfig_set_attr,
  786. .policy = switch_policy,
  787. },
  788. {
  789. .cmd = SWITCH_CMD_SET_PORT,
  790. .doit = swconfig_set_attr,
  791. .policy = switch_policy,
  792. },
  793. {
  794. .cmd = SWITCH_CMD_GET_SWITCH,
  795. .dumpit = swconfig_dump_switches,
  796. .policy = switch_policy,
  797. .done = swconfig_done,
  798. }
  799. };
  800. int
  801. register_switch(struct switch_dev *dev, struct net_device *netdev)
  802. {
  803. struct switch_dev *sdev;
  804. const int max_switches = 8 * sizeof(unsigned long);
  805. unsigned long in_use = 0;
  806. int err;
  807. int i;
  808. INIT_LIST_HEAD(&dev->dev_list);
  809. if (netdev) {
  810. dev->netdev = netdev;
  811. if (!dev->alias)
  812. dev->alias = netdev->name;
  813. }
  814. BUG_ON(!dev->alias);
  815. if (dev->ports > 0) {
  816. dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
  817. GFP_KERNEL);
  818. if (!dev->portbuf)
  819. return -ENOMEM;
  820. }
  821. swconfig_defaults_init(dev);
  822. mutex_init(&dev->sw_mutex);
  823. swconfig_lock();
  824. dev->id = ++swdev_id;
  825. list_for_each_entry(sdev, &swdevs, dev_list) {
  826. if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
  827. continue;
  828. if (i < 0 || i > max_switches)
  829. continue;
  830. set_bit(i, &in_use);
  831. }
  832. i = find_first_zero_bit(&in_use, max_switches);
  833. if (i == max_switches) {
  834. swconfig_unlock();
  835. return -ENFILE;
  836. }
  837. /* fill device name */
  838. snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
  839. list_add(&dev->dev_list, &swdevs);
  840. swconfig_unlock();
  841. err = swconfig_create_led_trigger(dev);
  842. if (err)
  843. return err;
  844. return 0;
  845. }
  846. EXPORT_SYMBOL_GPL(register_switch);
  847. void
  848. unregister_switch(struct switch_dev *dev)
  849. {
  850. swconfig_destroy_led_trigger(dev);
  851. kfree(dev->portbuf);
  852. mutex_lock(&dev->sw_mutex);
  853. swconfig_lock();
  854. list_del(&dev->dev_list);
  855. swconfig_unlock();
  856. mutex_unlock(&dev->sw_mutex);
  857. }
  858. EXPORT_SYMBOL_GPL(unregister_switch);
  859. static int __init
  860. swconfig_init(void)
  861. {
  862. int i, err;
  863. INIT_LIST_HEAD(&swdevs);
  864. err = genl_register_family(&switch_fam);
  865. if (err)
  866. return err;
  867. for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
  868. err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
  869. if (err)
  870. goto unregister;
  871. }
  872. return 0;
  873. unregister:
  874. genl_unregister_family(&switch_fam);
  875. return err;
  876. }
  877. static void __exit
  878. swconfig_exit(void)
  879. {
  880. genl_unregister_family(&switch_fam);
  881. }
  882. module_init(swconfig_init);
  883. module_exit(swconfig_exit);