swconfig.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. MODULE_AUTHOR("Felix Fietkau <[email protected]>");
  32. MODULE_LICENSE("GPL");
  33. static int swdev_id = 0;
  34. static struct list_head swdevs;
  35. static spinlock_t swdevs_lock = SPIN_LOCK_UNLOCKED;
  36. struct swconfig_callback;
  37. struct swconfig_callback
  38. {
  39. struct sk_buff *msg;
  40. struct genlmsghdr *hdr;
  41. struct genl_info *info;
  42. int cmd;
  43. /* callback for filling in the message data */
  44. int (*fill)(struct swconfig_callback *cb, void *arg);
  45. /* callback for closing the message before sending it */
  46. int (*close)(struct swconfig_callback *cb, void *arg);
  47. struct nlattr *nest[4];
  48. int args[4];
  49. };
  50. /* defaults */
  51. static int
  52. swconfig_get_vlan_ports(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  53. {
  54. int ret;
  55. if (val->port_vlan >= dev->vlans)
  56. return -EINVAL;
  57. if (!dev->get_vlan_ports)
  58. return -EOPNOTSUPP;
  59. ret = dev->get_vlan_ports(dev, val);
  60. printk("SET PORTS %d\n", val->len);
  61. return ret;
  62. }
  63. static int
  64. swconfig_set_vlan_ports(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  65. {
  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. for (i = 0; i < val->len; i++) {
  73. if (val->value.ports[i].id >= dev->ports)
  74. return -EINVAL;
  75. }
  76. if (!dev->set_vlan_ports)
  77. return -EOPNOTSUPP;
  78. printk("SET PORTS %d\n", val->len);
  79. return dev->set_vlan_ports(dev, val);
  80. }
  81. static int
  82. swconfig_apply_config(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
  83. {
  84. /* don't complain if not supported by the switch driver */
  85. if (!dev->apply_config)
  86. return 0;
  87. return dev->apply_config(dev);
  88. }
  89. enum global_defaults {
  90. GLOBAL_APPLY,
  91. };
  92. enum vlan_defaults {
  93. VLAN_PORTS,
  94. };
  95. enum port_defaults {
  96. PORT_LINK,
  97. };
  98. static struct switch_attr default_global[] = {
  99. [GLOBAL_APPLY] = {
  100. .type = SWITCH_TYPE_NOVAL,
  101. .name = "apply",
  102. .description = "Activate changes in the hardware",
  103. .set = swconfig_apply_config,
  104. }
  105. };
  106. static struct switch_attr default_port[] = {
  107. [PORT_LINK] = {
  108. .type = SWITCH_TYPE_INT,
  109. .name = "link",
  110. .description = "Current link speed",
  111. }
  112. };
  113. static struct switch_attr default_vlan[] = {
  114. [VLAN_PORTS] = {
  115. .type = SWITCH_TYPE_PORTS,
  116. .name = "ports",
  117. .description = "VLAN port mapping",
  118. .set = swconfig_set_vlan_ports,
  119. .get = swconfig_get_vlan_ports,
  120. },
  121. };
  122. static void swconfig_defaults_init(struct switch_dev *dev)
  123. {
  124. dev->def_global = 0;
  125. dev->def_vlan = 0;
  126. dev->def_port = 0;
  127. if (dev->get_vlan_ports || dev->set_vlan_ports)
  128. set_bit(VLAN_PORTS, &dev->def_vlan);
  129. /* always present, can be no-op */
  130. set_bit(GLOBAL_APPLY, &dev->def_global);
  131. }
  132. static struct genl_family switch_fam = {
  133. .id = GENL_ID_GENERATE,
  134. .name = "switch",
  135. .hdrsize = 0,
  136. .version = 1,
  137. .maxattr = SWITCH_ATTR_MAX,
  138. };
  139. static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
  140. [SWITCH_ATTR_ID] = { .type = NLA_U32 },
  141. [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
  142. [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
  143. [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
  144. [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
  145. [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
  146. [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
  147. [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
  148. };
  149. static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
  150. [SWITCH_PORT_ID] = { .type = NLA_U32 },
  151. [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
  152. };
  153. static inline void
  154. swconfig_lock(void)
  155. {
  156. spin_lock(&swdevs_lock);
  157. }
  158. static inline void
  159. swconfig_unlock(void)
  160. {
  161. spin_unlock(&swdevs_lock);
  162. }
  163. static struct switch_dev *
  164. swconfig_get_dev(struct genl_info *info)
  165. {
  166. struct switch_dev *dev = NULL;
  167. struct switch_dev *p;
  168. int id;
  169. if (!info->attrs[SWITCH_ATTR_ID])
  170. goto done;
  171. id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
  172. swconfig_lock();
  173. list_for_each_entry(p, &swdevs, dev_list) {
  174. if (id != p->id)
  175. continue;
  176. dev = p;
  177. break;
  178. }
  179. if (dev)
  180. spin_lock(&dev->lock);
  181. else
  182. DPRINTF("device %d not found\n", id);
  183. swconfig_unlock();
  184. done:
  185. return dev;
  186. }
  187. static inline void
  188. swconfig_put_dev(struct switch_dev *dev)
  189. {
  190. spin_unlock(&dev->lock);
  191. }
  192. static int
  193. swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
  194. {
  195. struct switch_attr *op = arg;
  196. struct genl_info *info = cb->info;
  197. struct sk_buff *msg = cb->msg;
  198. int id = cb->args[0];
  199. void *hdr;
  200. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
  201. NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
  202. if (IS_ERR(hdr))
  203. return -1;
  204. NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
  205. NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
  206. NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
  207. if (op->description)
  208. NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
  209. op->description);
  210. return genlmsg_end(msg, hdr);
  211. nla_put_failure:
  212. genlmsg_cancel(msg, hdr);
  213. return -EMSGSIZE;
  214. }
  215. /* spread multipart messages across multiple message buffers */
  216. static int
  217. swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
  218. {
  219. struct genl_info *info = cb->info;
  220. int restart = 0;
  221. int err;
  222. do {
  223. if (!cb->msg) {
  224. cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  225. if (cb->msg == NULL)
  226. goto error;
  227. }
  228. if (!(cb->fill(cb, arg) < 0))
  229. break;
  230. /* fill failed, check if this was already the second attempt */
  231. if (restart)
  232. goto error;
  233. /* try again in a new message, send the current one */
  234. restart = 1;
  235. if (cb->close) {
  236. if (cb->close(cb, arg) < 0)
  237. goto error;
  238. }
  239. err = genlmsg_unicast(cb->msg, info->snd_pid);
  240. cb->msg = NULL;
  241. if (err < 0)
  242. goto error;
  243. } while (restart);
  244. return 0;
  245. error:
  246. if (cb->msg)
  247. nlmsg_free(cb->msg);
  248. return -1;
  249. }
  250. static int
  251. swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
  252. {
  253. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  254. const struct switch_attrlist *alist;
  255. struct switch_dev *dev;
  256. struct swconfig_callback cb;
  257. int err = -EINVAL;
  258. int i;
  259. /* defaults */
  260. struct switch_attr *def_list;
  261. unsigned long *def_active;
  262. int n_def;
  263. dev = swconfig_get_dev(info);
  264. if (!dev)
  265. return -EINVAL;
  266. switch(hdr->cmd) {
  267. case SWITCH_CMD_LIST_GLOBAL:
  268. alist = &dev->attr_global;
  269. def_list = default_global;
  270. def_active = &dev->def_global;
  271. n_def = ARRAY_SIZE(default_global);
  272. break;
  273. case SWITCH_CMD_LIST_VLAN:
  274. alist = &dev->attr_vlan;
  275. def_list = default_vlan;
  276. def_active = &dev->def_vlan;
  277. n_def = ARRAY_SIZE(default_vlan);
  278. break;
  279. case SWITCH_CMD_LIST_PORT:
  280. alist = &dev->attr_port;
  281. def_list = default_port;
  282. def_active = &dev->def_port;
  283. n_def = ARRAY_SIZE(default_port);
  284. break;
  285. default:
  286. WARN_ON(1);
  287. goto out;
  288. }
  289. memset(&cb, 0, sizeof(cb));
  290. cb.info = info;
  291. cb.fill = swconfig_dump_attr;
  292. for (i = 0; i < alist->n_attr; i++) {
  293. if (alist->attr[i].disabled)
  294. continue;
  295. cb.args[0] = i;
  296. err = swconfig_send_multipart(&cb, &alist->attr[i]);
  297. if (err < 0)
  298. goto error;
  299. }
  300. /* defaults */
  301. for (i = 0; i < n_def; i++) {
  302. if (!test_bit(i, def_active))
  303. continue;
  304. cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
  305. err = swconfig_send_multipart(&cb, &def_list[i]);
  306. if (err < 0)
  307. goto error;
  308. }
  309. swconfig_put_dev(dev);
  310. if (!cb.msg)
  311. return 0;
  312. return genlmsg_unicast(cb.msg, info->snd_pid);
  313. error:
  314. if (cb.msg)
  315. nlmsg_free(cb.msg);
  316. out:
  317. swconfig_put_dev(dev);
  318. return err;
  319. }
  320. static struct switch_attr *
  321. swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
  322. struct switch_val *val)
  323. {
  324. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  325. const struct switch_attrlist *alist;
  326. struct switch_attr *attr = NULL;
  327. int attr_id;
  328. /* defaults */
  329. struct switch_attr *def_list;
  330. unsigned long *def_active;
  331. int n_def;
  332. if (!info->attrs[SWITCH_ATTR_OP_ID])
  333. goto done;
  334. switch(hdr->cmd) {
  335. case SWITCH_CMD_SET_GLOBAL:
  336. case SWITCH_CMD_GET_GLOBAL:
  337. alist = &dev->attr_global;
  338. def_list = default_global;
  339. def_active = &dev->def_global;
  340. n_def = ARRAY_SIZE(default_global);
  341. break;
  342. case SWITCH_CMD_SET_VLAN:
  343. case SWITCH_CMD_GET_VLAN:
  344. alist = &dev->attr_vlan;
  345. def_list = default_vlan;
  346. def_active = &dev->def_vlan;
  347. n_def = ARRAY_SIZE(default_vlan);
  348. if (!info->attrs[SWITCH_ATTR_OP_VLAN])
  349. goto done;
  350. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
  351. break;
  352. case SWITCH_CMD_SET_PORT:
  353. case SWITCH_CMD_GET_PORT:
  354. alist = &dev->attr_port;
  355. def_list = default_port;
  356. def_active = &dev->def_port;
  357. n_def = ARRAY_SIZE(default_port);
  358. if (!info->attrs[SWITCH_ATTR_OP_PORT])
  359. goto done;
  360. val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
  361. break;
  362. default:
  363. WARN_ON(1);
  364. goto done;
  365. }
  366. if (!alist)
  367. goto done;
  368. attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
  369. if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
  370. attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
  371. if (attr_id >= n_def)
  372. goto done;
  373. if (!test_bit(attr_id, def_active))
  374. goto done;
  375. attr = &def_list[attr_id];
  376. } else {
  377. if (attr_id >= alist->n_attr)
  378. goto done;
  379. attr = &alist->attr[attr_id];
  380. }
  381. if (attr->disabled)
  382. attr = NULL;
  383. done:
  384. if (!attr)
  385. DPRINTF("attribute lookup failed\n");
  386. val->attr = attr;
  387. return attr;
  388. }
  389. static int
  390. swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
  391. struct switch_val *val, int max)
  392. {
  393. struct nlattr *nla;
  394. int rem;
  395. val->len = 0;
  396. nla_for_each_nested(nla, head, rem) {
  397. struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
  398. struct switch_port *port = &val->value.ports[val->len];
  399. if (val->len >= max)
  400. return -EINVAL;
  401. if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
  402. port_policy))
  403. return -EINVAL;
  404. if (!tb[SWITCH_PORT_ID])
  405. return -EINVAL;
  406. port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
  407. if (tb[SWITCH_PORT_FLAG_TAGGED])
  408. port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
  409. val->len++;
  410. }
  411. return 0;
  412. }
  413. static int
  414. swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
  415. {
  416. struct switch_attr *attr;
  417. struct switch_dev *dev;
  418. struct switch_val val;
  419. int err = -EINVAL;
  420. dev = swconfig_get_dev(info);
  421. if (!dev)
  422. return -EINVAL;
  423. memset(&val, 0, sizeof(val));
  424. attr = swconfig_lookup_attr(dev, info, &val);
  425. if (!attr || !attr->set)
  426. goto error;
  427. val.attr = attr;
  428. switch(attr->type) {
  429. case SWITCH_TYPE_NOVAL:
  430. break;
  431. case SWITCH_TYPE_INT:
  432. if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
  433. goto error;
  434. val.value.i =
  435. nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
  436. break;
  437. case SWITCH_TYPE_STRING:
  438. if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
  439. goto error;
  440. val.value.s =
  441. nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
  442. break;
  443. case SWITCH_TYPE_PORTS:
  444. val.value.ports = dev->portbuf;
  445. memset(dev->portbuf, 0,
  446. sizeof(struct switch_port) * dev->ports);
  447. /* TODO: implement multipart? */
  448. if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
  449. err = swconfig_parse_ports(skb,
  450. info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
  451. if (err < 0)
  452. goto error;
  453. } else {
  454. val.len = 0;
  455. err = 0;
  456. }
  457. break;
  458. default:
  459. goto error;
  460. }
  461. err = attr->set(dev, attr, &val);
  462. error:
  463. swconfig_put_dev(dev);
  464. return err;
  465. }
  466. static int
  467. swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
  468. {
  469. if (cb->nest[0])
  470. nla_nest_end(cb->msg, cb->nest[0]);
  471. return 0;
  472. }
  473. static int
  474. swconfig_send_port(struct swconfig_callback *cb, void *arg)
  475. {
  476. const struct switch_port *port = arg;
  477. struct nlattr *p = NULL;
  478. if (!cb->nest[0]) {
  479. cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
  480. if (!cb->nest[0])
  481. return -1;
  482. }
  483. p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
  484. if (!p)
  485. goto error;
  486. NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
  487. if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
  488. NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
  489. nla_nest_end(cb->msg, p);
  490. return 0;
  491. nla_put_failure:
  492. nla_nest_cancel(cb->msg, p);
  493. error:
  494. nla_nest_cancel(cb->msg, cb->nest[0]);
  495. return -1;
  496. }
  497. static int
  498. swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
  499. const struct switch_val *val)
  500. {
  501. struct swconfig_callback cb;
  502. int err = 0;
  503. int i;
  504. if (!val->value.ports)
  505. return -EINVAL;
  506. memset(&cb, 0, sizeof(cb));
  507. cb.cmd = attr;
  508. cb.msg = *msg;
  509. cb.info = info;
  510. cb.fill = swconfig_send_port;
  511. cb.close = swconfig_close_portlist;
  512. cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
  513. for (i = 0; i < val->len; i++) {
  514. err = swconfig_send_multipart(&cb, &val->value.ports[i]);
  515. if (err)
  516. goto done;
  517. }
  518. err = val->len;
  519. swconfig_close_portlist(&cb, NULL);
  520. *msg = cb.msg;
  521. done:
  522. return err;
  523. }
  524. static int
  525. swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
  526. {
  527. struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  528. struct switch_attr *attr;
  529. struct switch_dev *dev;
  530. struct sk_buff *msg = NULL;
  531. struct switch_val val;
  532. int err = -EINVAL;
  533. int cmd = hdr->cmd;
  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->get)
  540. goto error_dev;
  541. if (attr->type == SWITCH_TYPE_PORTS) {
  542. val.value.ports = dev->portbuf;
  543. memset(dev->portbuf, 0,
  544. sizeof(struct switch_port) * dev->ports);
  545. }
  546. err = attr->get(dev, attr, &val);
  547. if (err)
  548. goto error;
  549. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  550. if (!msg)
  551. goto error;
  552. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
  553. 0, cmd);
  554. if (IS_ERR(hdr))
  555. goto nla_put_failure;
  556. switch(attr->type) {
  557. case SWITCH_TYPE_INT:
  558. NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
  559. break;
  560. case SWITCH_TYPE_STRING:
  561. NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
  562. break;
  563. case SWITCH_TYPE_PORTS:
  564. err = swconfig_send_ports(&msg, info,
  565. SWITCH_ATTR_OP_VALUE_PORTS, &val);
  566. if (err < 0)
  567. goto nla_put_failure;
  568. break;
  569. default:
  570. DPRINTF("invalid type in attribute\n");
  571. err = -EINVAL;
  572. goto error;
  573. }
  574. err = genlmsg_end(msg, hdr);
  575. if (err < 0)
  576. goto nla_put_failure;
  577. swconfig_put_dev(dev);
  578. return genlmsg_unicast(msg, info->snd_pid);
  579. nla_put_failure:
  580. if (msg)
  581. nlmsg_free(msg);
  582. error_dev:
  583. swconfig_put_dev(dev);
  584. error:
  585. if (!err)
  586. err = -ENOMEM;
  587. return err;
  588. }
  589. static int
  590. swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
  591. const struct switch_dev *dev)
  592. {
  593. void *hdr;
  594. hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
  595. SWITCH_CMD_NEW_ATTR);
  596. if (IS_ERR(hdr))
  597. return -1;
  598. NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
  599. NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
  600. NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
  601. NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
  602. NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
  603. return genlmsg_end(msg, hdr);
  604. nla_put_failure:
  605. genlmsg_cancel(msg, hdr);
  606. return -EMSGSIZE;
  607. }
  608. static int swconfig_dump_switches(struct sk_buff *skb,
  609. struct netlink_callback *cb)
  610. {
  611. struct switch_dev *dev;
  612. int start = cb->args[0];
  613. int idx = 0;
  614. swconfig_lock();
  615. list_for_each_entry(dev, &swdevs, dev_list) {
  616. if (++idx <= start)
  617. continue;
  618. if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
  619. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  620. dev) < 0)
  621. break;
  622. }
  623. swconfig_unlock();
  624. cb->args[0] = idx;
  625. return skb->len;
  626. }
  627. static int
  628. swconfig_done(struct netlink_callback *cb)
  629. {
  630. return 0;
  631. }
  632. static struct genl_ops swconfig_ops[] = {
  633. {
  634. .cmd = SWITCH_CMD_LIST_GLOBAL,
  635. .doit = swconfig_list_attrs,
  636. .policy = switch_policy,
  637. },
  638. {
  639. .cmd = SWITCH_CMD_LIST_VLAN,
  640. .doit = swconfig_list_attrs,
  641. .policy = switch_policy,
  642. },
  643. {
  644. .cmd = SWITCH_CMD_LIST_PORT,
  645. .doit = swconfig_list_attrs,
  646. .policy = switch_policy,
  647. },
  648. {
  649. .cmd = SWITCH_CMD_GET_GLOBAL,
  650. .doit = swconfig_get_attr,
  651. .policy = switch_policy,
  652. },
  653. {
  654. .cmd = SWITCH_CMD_GET_VLAN,
  655. .doit = swconfig_get_attr,
  656. .policy = switch_policy,
  657. },
  658. {
  659. .cmd = SWITCH_CMD_GET_PORT,
  660. .doit = swconfig_get_attr,
  661. .policy = switch_policy,
  662. },
  663. {
  664. .cmd = SWITCH_CMD_SET_GLOBAL,
  665. .doit = swconfig_set_attr,
  666. .policy = switch_policy,
  667. },
  668. {
  669. .cmd = SWITCH_CMD_SET_VLAN,
  670. .doit = swconfig_set_attr,
  671. .policy = switch_policy,
  672. },
  673. {
  674. .cmd = SWITCH_CMD_SET_PORT,
  675. .doit = swconfig_set_attr,
  676. .policy = switch_policy,
  677. },
  678. {
  679. .cmd = SWITCH_CMD_GET_SWITCH,
  680. .dumpit = swconfig_dump_switches,
  681. .policy = switch_policy,
  682. .done = swconfig_done,
  683. }
  684. };
  685. int
  686. register_switch(struct switch_dev *dev, struct net_device *netdev)
  687. {
  688. INIT_LIST_HEAD(&dev->dev_list);
  689. if (netdev) {
  690. dev->netdev = netdev;
  691. if (!dev->devname)
  692. dev->devname = netdev->name;
  693. }
  694. BUG_ON(!dev->devname);
  695. if (dev->ports > 0) {
  696. dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
  697. GFP_KERNEL);
  698. if (!dev->portbuf)
  699. return -ENOMEM;
  700. }
  701. dev->id = ++swdev_id;
  702. swconfig_defaults_init(dev);
  703. spin_lock_init(&dev->lock);
  704. swconfig_lock();
  705. list_add(&dev->dev_list, &swdevs);
  706. swconfig_unlock();
  707. return 0;
  708. }
  709. EXPORT_SYMBOL_GPL(register_switch);
  710. void
  711. unregister_switch(struct switch_dev *dev)
  712. {
  713. kfree(dev->portbuf);
  714. spin_lock(&dev->lock);
  715. swconfig_lock();
  716. list_del(&dev->dev_list);
  717. swconfig_unlock();
  718. }
  719. EXPORT_SYMBOL_GPL(unregister_switch);
  720. static int __init
  721. swconfig_init(void)
  722. {
  723. int i, err;
  724. INIT_LIST_HEAD(&swdevs);
  725. err = genl_register_family(&switch_fam);
  726. if (err)
  727. return err;
  728. for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
  729. err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
  730. if (err)
  731. goto unregister;
  732. }
  733. return 0;
  734. unregister:
  735. genl_unregister_family(&switch_fam);
  736. return err;
  737. }
  738. static void __exit
  739. swconfig_exit(void)
  740. {
  741. genl_unregister_family(&switch_fam);
  742. }
  743. module_init(swconfig_init);
  744. module_exit(swconfig_exit);