swconfig.c 27 KB

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