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