OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / s390 / net / qeth_l3_sys.c
blobd979bb26522ff8348df72caa8d2c5871deb7f5b4
1 /*
2 * drivers/s390/net/qeth_l3_sys.c
4 * Copyright IBM Corp. 2007
5 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6 * Frank Pavlic <fpavlic@de.ibm.com>,
7 * Thomas Spatzier <tspat@de.ibm.com>,
8 * Frank Blaschka <frank.blaschka@de.ibm.com>
9 */
11 #include <linux/slab.h>
12 #include <asm/ebcdic.h>
13 #include "qeth_l3.h"
15 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \
16 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store)
18 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card,
19 struct qeth_routing_info *route, char *buf)
21 switch (route->type) {
22 case PRIMARY_ROUTER:
23 return sprintf(buf, "%s\n", "primary router");
24 case SECONDARY_ROUTER:
25 return sprintf(buf, "%s\n", "secondary router");
26 case MULTICAST_ROUTER:
27 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
28 return sprintf(buf, "%s\n", "multicast router+");
29 else
30 return sprintf(buf, "%s\n", "multicast router");
31 case PRIMARY_CONNECTOR:
32 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
33 return sprintf(buf, "%s\n", "primary connector+");
34 else
35 return sprintf(buf, "%s\n", "primary connector");
36 case SECONDARY_CONNECTOR:
37 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
38 return sprintf(buf, "%s\n", "secondary connector+");
39 else
40 return sprintf(buf, "%s\n", "secondary connector");
41 default:
42 return sprintf(buf, "%s\n", "no");
46 static ssize_t qeth_l3_dev_route4_show(struct device *dev,
47 struct device_attribute *attr, char *buf)
49 struct qeth_card *card = dev_get_drvdata(dev);
51 if (!card)
52 return -EINVAL;
54 return qeth_l3_dev_route_show(card, &card->options.route4, buf);
57 static ssize_t qeth_l3_dev_route_store(struct qeth_card *card,
58 struct qeth_routing_info *route, enum qeth_prot_versions prot,
59 const char *buf, size_t count)
61 enum qeth_routing_types old_route_type = route->type;
62 char *tmp;
63 int rc = 0;
65 tmp = strsep((char **) &buf, "\n");
66 mutex_lock(&card->conf_mutex);
67 if (!strcmp(tmp, "no_router")) {
68 route->type = NO_ROUTER;
69 } else if (!strcmp(tmp, "primary_connector")) {
70 route->type = PRIMARY_CONNECTOR;
71 } else if (!strcmp(tmp, "secondary_connector")) {
72 route->type = SECONDARY_CONNECTOR;
73 } else if (!strcmp(tmp, "primary_router")) {
74 route->type = PRIMARY_ROUTER;
75 } else if (!strcmp(tmp, "secondary_router")) {
76 route->type = SECONDARY_ROUTER;
77 } else if (!strcmp(tmp, "multicast_router")) {
78 route->type = MULTICAST_ROUTER;
79 } else {
80 rc = -EINVAL;
81 goto out;
83 if (((card->state == CARD_STATE_SOFTSETUP) ||
84 (card->state == CARD_STATE_UP)) &&
85 (old_route_type != route->type)) {
86 if (prot == QETH_PROT_IPV4)
87 rc = qeth_l3_setrouting_v4(card);
88 else if (prot == QETH_PROT_IPV6)
89 rc = qeth_l3_setrouting_v6(card);
91 out:
92 mutex_unlock(&card->conf_mutex);
93 return rc ? rc : count;
96 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
97 struct device_attribute *attr, const char *buf, size_t count)
99 struct qeth_card *card = dev_get_drvdata(dev);
101 if (!card)
102 return -EINVAL;
104 return qeth_l3_dev_route_store(card, &card->options.route4,
105 QETH_PROT_IPV4, buf, count);
108 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
109 qeth_l3_dev_route4_store);
111 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 struct qeth_card *card = dev_get_drvdata(dev);
116 if (!card)
117 return -EINVAL;
119 return qeth_l3_dev_route_show(card, &card->options.route6, buf);
122 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
123 struct device_attribute *attr, const char *buf, size_t count)
125 struct qeth_card *card = dev_get_drvdata(dev);
127 if (!card)
128 return -EINVAL;
130 return qeth_l3_dev_route_store(card, &card->options.route6,
131 QETH_PROT_IPV6, buf, count);
134 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
135 qeth_l3_dev_route6_store);
137 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
140 struct qeth_card *card = dev_get_drvdata(dev);
142 if (!card)
143 return -EINVAL;
145 return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
148 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count)
151 struct qeth_card *card = dev_get_drvdata(dev);
152 char *tmp;
153 int i, rc = 0;
155 if (!card)
156 return -EINVAL;
158 mutex_lock(&card->conf_mutex);
159 if ((card->state != CARD_STATE_DOWN) &&
160 (card->state != CARD_STATE_RECOVER)) {
161 rc = -EPERM;
162 goto out;
165 i = simple_strtoul(buf, &tmp, 16);
166 if ((i == 0) || (i == 1))
167 card->options.fake_broadcast = i;
168 else
169 rc = -EINVAL;
170 out:
171 mutex_unlock(&card->conf_mutex);
172 return rc ? rc : count;
175 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
176 qeth_l3_dev_fake_broadcast_store);
178 static ssize_t qeth_l3_dev_broadcast_mode_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct qeth_card *card = dev_get_drvdata(dev);
183 if (!card)
184 return -EINVAL;
186 if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
187 (card->info.link_type == QETH_LINK_TYPE_LANE_TR)))
188 return sprintf(buf, "n/a\n");
190 return sprintf(buf, "%s\n", (card->options.broadcast_mode ==
191 QETH_TR_BROADCAST_ALLRINGS)?
192 "all rings":"local");
195 static ssize_t qeth_l3_dev_broadcast_mode_store(struct device *dev,
196 struct device_attribute *attr, const char *buf, size_t count)
198 struct qeth_card *card = dev_get_drvdata(dev);
199 char *tmp;
200 int rc = 0;
202 if (!card)
203 return -EINVAL;
205 mutex_lock(&card->conf_mutex);
206 if ((card->state != CARD_STATE_DOWN) &&
207 (card->state != CARD_STATE_RECOVER)) {
208 rc = -EPERM;
209 goto out;
212 if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
213 (card->info.link_type == QETH_LINK_TYPE_LANE_TR))) {
214 rc = -EINVAL;
215 goto out;
218 tmp = strsep((char **) &buf, "\n");
220 if (!strcmp(tmp, "local"))
221 card->options.broadcast_mode = QETH_TR_BROADCAST_LOCAL;
222 else if (!strcmp(tmp, "all_rings"))
223 card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
224 else
225 rc = -EINVAL;
226 out:
227 mutex_unlock(&card->conf_mutex);
228 return rc ? rc : count;
231 static DEVICE_ATTR(broadcast_mode, 0644, qeth_l3_dev_broadcast_mode_show,
232 qeth_l3_dev_broadcast_mode_store);
234 static ssize_t qeth_l3_dev_canonical_macaddr_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct qeth_card *card = dev_get_drvdata(dev);
239 if (!card)
240 return -EINVAL;
242 if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
243 (card->info.link_type == QETH_LINK_TYPE_LANE_TR)))
244 return sprintf(buf, "n/a\n");
246 return sprintf(buf, "%i\n", (card->options.macaddr_mode ==
247 QETH_TR_MACADDR_CANONICAL)? 1:0);
250 static ssize_t qeth_l3_dev_canonical_macaddr_store(struct device *dev,
251 struct device_attribute *attr, const char *buf, size_t count)
253 struct qeth_card *card = dev_get_drvdata(dev);
254 char *tmp;
255 int i, rc = 0;
257 if (!card)
258 return -EINVAL;
260 mutex_lock(&card->conf_mutex);
261 if ((card->state != CARD_STATE_DOWN) &&
262 (card->state != CARD_STATE_RECOVER)) {
263 rc = -EPERM;
264 goto out;
267 if (!((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
268 (card->info.link_type == QETH_LINK_TYPE_LANE_TR))) {
269 rc = -EINVAL;
270 goto out;
273 i = simple_strtoul(buf, &tmp, 16);
274 if ((i == 0) || (i == 1))
275 card->options.macaddr_mode = i?
276 QETH_TR_MACADDR_CANONICAL :
277 QETH_TR_MACADDR_NONCANONICAL;
278 else
279 rc = -EINVAL;
280 out:
281 mutex_unlock(&card->conf_mutex);
282 return rc ? rc : count;
285 static DEVICE_ATTR(canonical_macaddr, 0644, qeth_l3_dev_canonical_macaddr_show,
286 qeth_l3_dev_canonical_macaddr_store);
288 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 struct qeth_card *card = dev_get_drvdata(dev);
293 if (!card)
294 return -EINVAL;
296 return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
299 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
300 struct device_attribute *attr, const char *buf, size_t count)
302 struct qeth_card *card = dev_get_drvdata(dev);
303 int rc = 0;
304 unsigned long i;
306 if (!card)
307 return -EINVAL;
309 if (card->info.type != QETH_CARD_TYPE_IQD)
310 return -EPERM;
311 if (card->options.cq == QETH_CQ_ENABLED)
312 return -EPERM;
314 mutex_lock(&card->conf_mutex);
315 if ((card->state != CARD_STATE_DOWN) &&
316 (card->state != CARD_STATE_RECOVER)) {
317 rc = -EPERM;
318 goto out;
321 rc = strict_strtoul(buf, 16, &i);
322 if (rc) {
323 rc = -EINVAL;
324 goto out;
326 switch (i) {
327 case 0:
328 card->options.sniffer = i;
329 break;
330 case 1:
331 qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
332 if (card->ssqd.qdioac2 & QETH_SNIFF_AVAIL) {
333 card->options.sniffer = i;
334 if (card->qdio.init_pool.buf_count !=
335 QETH_IN_BUF_COUNT_MAX)
336 qeth_realloc_buffer_pool(card,
337 QETH_IN_BUF_COUNT_MAX);
338 } else
339 rc = -EPERM;
340 break;
341 default:
342 rc = -EINVAL;
344 out:
345 mutex_unlock(&card->conf_mutex);
346 return rc ? rc : count;
349 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
350 qeth_l3_dev_sniffer_store);
353 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
356 struct qeth_card *card = dev_get_drvdata(dev);
357 char tmp_hsuid[9];
359 if (!card)
360 return -EINVAL;
362 if (card->info.type != QETH_CARD_TYPE_IQD)
363 return -EPERM;
365 if (card->state == CARD_STATE_DOWN)
366 return -EPERM;
368 memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
369 EBCASC(tmp_hsuid, 8);
370 return sprintf(buf, "%s\n", tmp_hsuid);
373 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
374 struct device_attribute *attr, const char *buf, size_t count)
376 struct qeth_card *card = dev_get_drvdata(dev);
377 struct qeth_ipaddr *addr;
378 char *tmp;
379 int i;
381 if (!card)
382 return -EINVAL;
384 if (card->info.type != QETH_CARD_TYPE_IQD)
385 return -EPERM;
386 if (card->state != CARD_STATE_DOWN &&
387 card->state != CARD_STATE_RECOVER)
388 return -EPERM;
389 if (card->options.sniffer)
390 return -EPERM;
391 if (card->options.cq == QETH_CQ_NOTAVAILABLE)
392 return -EPERM;
394 tmp = strsep((char **)&buf, "\n");
395 if (strlen(tmp) > 8)
396 return -EINVAL;
398 if (card->options.hsuid[0]) {
399 /* delete old ip address */
400 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
401 if (addr != NULL) {
402 addr->u.a6.addr.s6_addr32[0] = 0xfe800000;
403 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
404 for (i = 8; i < 16; i++)
405 addr->u.a6.addr.s6_addr[i] =
406 card->options.hsuid[i - 8];
407 addr->u.a6.pfxlen = 0;
408 addr->type = QETH_IP_TYPE_NORMAL;
409 } else
410 return -ENOMEM;
411 if (!qeth_l3_delete_ip(card, addr))
412 kfree(addr);
413 qeth_l3_set_ip_addr_list(card);
416 if (strlen(tmp) == 0) {
417 /* delete ip address only */
418 card->options.hsuid[0] = '\0';
419 if (card->dev)
420 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
421 qeth_configure_cq(card, QETH_CQ_DISABLED);
422 return count;
425 if (qeth_configure_cq(card, QETH_CQ_ENABLED))
426 return -EPERM;
428 for (i = 0; i < 8; i++)
429 card->options.hsuid[i] = ' ';
430 card->options.hsuid[8] = '\0';
431 strncpy(card->options.hsuid, tmp, strlen(tmp));
432 ASCEBC(card->options.hsuid, 8);
433 if (card->dev)
434 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
436 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
437 if (addr != NULL) {
438 addr->u.a6.addr.s6_addr32[0] = 0xfe800000;
439 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
440 for (i = 8; i < 16; i++)
441 addr->u.a6.addr.s6_addr[i] = card->options.hsuid[i - 8];
442 addr->u.a6.pfxlen = 0;
443 addr->type = QETH_IP_TYPE_NORMAL;
444 } else
445 return -ENOMEM;
446 if (!qeth_l3_add_ip(card, addr))
447 kfree(addr);
448 qeth_l3_set_ip_addr_list(card);
450 return count;
453 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
454 qeth_l3_dev_hsuid_store);
457 static struct attribute *qeth_l3_device_attrs[] = {
458 &dev_attr_route4.attr,
459 &dev_attr_route6.attr,
460 &dev_attr_fake_broadcast.attr,
461 &dev_attr_broadcast_mode.attr,
462 &dev_attr_canonical_macaddr.attr,
463 &dev_attr_sniffer.attr,
464 &dev_attr_hsuid.attr,
465 NULL,
468 static struct attribute_group qeth_l3_device_attr_group = {
469 .attrs = qeth_l3_device_attrs,
472 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
475 struct qeth_card *card = dev_get_drvdata(dev);
477 if (!card)
478 return -EINVAL;
480 return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
483 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
484 struct device_attribute *attr, const char *buf, size_t count)
486 struct qeth_card *card = dev_get_drvdata(dev);
487 struct qeth_ipaddr *tmpipa, *t;
488 char *tmp;
489 int rc = 0;
491 if (!card)
492 return -EINVAL;
494 mutex_lock(&card->conf_mutex);
495 if ((card->state != CARD_STATE_DOWN) &&
496 (card->state != CARD_STATE_RECOVER)) {
497 rc = -EPERM;
498 goto out;
501 tmp = strsep((char **) &buf, "\n");
502 if (!strcmp(tmp, "toggle")) {
503 card->ipato.enabled = (card->ipato.enabled)? 0 : 1;
504 } else if (!strcmp(tmp, "1")) {
505 card->ipato.enabled = 1;
506 list_for_each_entry_safe(tmpipa, t, card->ip_tbd_list, entry) {
507 if ((tmpipa->type == QETH_IP_TYPE_NORMAL) &&
508 qeth_l3_is_addr_covered_by_ipato(card, tmpipa))
509 tmpipa->set_flags |=
510 QETH_IPA_SETIP_TAKEOVER_FLAG;
513 } else if (!strcmp(tmp, "0")) {
514 card->ipato.enabled = 0;
515 list_for_each_entry_safe(tmpipa, t, card->ip_tbd_list, entry) {
516 if (tmpipa->set_flags &
517 QETH_IPA_SETIP_TAKEOVER_FLAG)
518 tmpipa->set_flags &=
519 ~QETH_IPA_SETIP_TAKEOVER_FLAG;
521 } else
522 rc = -EINVAL;
523 out:
524 mutex_unlock(&card->conf_mutex);
525 return rc ? rc : count;
528 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
529 qeth_l3_dev_ipato_enable_show,
530 qeth_l3_dev_ipato_enable_store);
532 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct qeth_card *card = dev_get_drvdata(dev);
537 if (!card)
538 return -EINVAL;
540 return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
543 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
547 struct qeth_card *card = dev_get_drvdata(dev);
548 char *tmp;
549 int rc = 0;
551 if (!card)
552 return -EINVAL;
554 mutex_lock(&card->conf_mutex);
555 tmp = strsep((char **) &buf, "\n");
556 if (!strcmp(tmp, "toggle")) {
557 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
558 } else if (!strcmp(tmp, "1")) {
559 card->ipato.invert4 = 1;
560 } else if (!strcmp(tmp, "0")) {
561 card->ipato.invert4 = 0;
562 } else
563 rc = -EINVAL;
564 mutex_unlock(&card->conf_mutex);
565 return rc ? rc : count;
568 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
569 qeth_l3_dev_ipato_invert4_show,
570 qeth_l3_dev_ipato_invert4_store);
572 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
573 enum qeth_prot_versions proto)
575 struct qeth_ipato_entry *ipatoe;
576 unsigned long flags;
577 char addr_str[40];
578 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
579 int i = 0;
581 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
582 /* add strlen for "/<mask>\n" */
583 entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
584 spin_lock_irqsave(&card->ip_lock, flags);
585 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
586 if (ipatoe->proto != proto)
587 continue;
588 /* String must not be longer than PAGE_SIZE. So we check if
589 * string length gets near PAGE_SIZE. Then we can savely display
590 * the next IPv6 address (worst case, compared to IPv4) */
591 if ((PAGE_SIZE - i) <= entry_len)
592 break;
593 qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
594 i += snprintf(buf + i, PAGE_SIZE - i,
595 "%s/%i\n", addr_str, ipatoe->mask_bits);
597 spin_unlock_irqrestore(&card->ip_lock, flags);
598 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
600 return i;
603 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
606 struct qeth_card *card = dev_get_drvdata(dev);
608 if (!card)
609 return -EINVAL;
611 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
614 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
615 u8 *addr, int *mask_bits)
617 const char *start, *end;
618 char *tmp;
619 char buffer[40] = {0, };
621 start = buf;
622 /* get address string */
623 end = strchr(start, '/');
624 if (!end || (end - start >= 40)) {
625 return -EINVAL;
627 strncpy(buffer, start, end - start);
628 if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
629 return -EINVAL;
631 start = end + 1;
632 *mask_bits = simple_strtoul(start, &tmp, 10);
633 if (!strlen(start) ||
634 (tmp == start) ||
635 (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
636 return -EINVAL;
638 return 0;
641 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
642 struct qeth_card *card, enum qeth_prot_versions proto)
644 struct qeth_ipato_entry *ipatoe;
645 u8 addr[16];
646 int mask_bits;
647 int rc = 0;
649 mutex_lock(&card->conf_mutex);
650 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
651 if (rc)
652 goto out;
654 ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
655 if (!ipatoe) {
656 rc = -ENOMEM;
657 goto out;
659 ipatoe->proto = proto;
660 memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
661 ipatoe->mask_bits = mask_bits;
663 rc = qeth_l3_add_ipato_entry(card, ipatoe);
664 if (rc)
665 kfree(ipatoe);
666 out:
667 mutex_unlock(&card->conf_mutex);
668 return rc ? rc : count;
671 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
672 struct device_attribute *attr, const char *buf, size_t count)
674 struct qeth_card *card = dev_get_drvdata(dev);
676 if (!card)
677 return -EINVAL;
679 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
682 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
683 qeth_l3_dev_ipato_add4_show,
684 qeth_l3_dev_ipato_add4_store);
686 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
687 struct qeth_card *card, enum qeth_prot_versions proto)
689 u8 addr[16];
690 int mask_bits;
691 int rc = 0;
693 mutex_lock(&card->conf_mutex);
694 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
695 if (!rc)
696 qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
697 mutex_unlock(&card->conf_mutex);
698 return rc ? rc : count;
701 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
702 struct device_attribute *attr, const char *buf, size_t count)
704 struct qeth_card *card = dev_get_drvdata(dev);
706 if (!card)
707 return -EINVAL;
709 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
712 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
713 qeth_l3_dev_ipato_del4_store);
715 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
716 struct device_attribute *attr, char *buf)
718 struct qeth_card *card = dev_get_drvdata(dev);
720 if (!card)
721 return -EINVAL;
723 return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
726 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
727 struct device_attribute *attr, const char *buf, size_t count)
729 struct qeth_card *card = dev_get_drvdata(dev);
730 char *tmp;
731 int rc = 0;
733 if (!card)
734 return -EINVAL;
736 mutex_lock(&card->conf_mutex);
737 tmp = strsep((char **) &buf, "\n");
738 if (!strcmp(tmp, "toggle")) {
739 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
740 } else if (!strcmp(tmp, "1")) {
741 card->ipato.invert6 = 1;
742 } else if (!strcmp(tmp, "0")) {
743 card->ipato.invert6 = 0;
744 } else
745 rc = -EINVAL;
746 mutex_unlock(&card->conf_mutex);
747 return rc ? rc : count;
750 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
751 qeth_l3_dev_ipato_invert6_show,
752 qeth_l3_dev_ipato_invert6_store);
755 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
758 struct qeth_card *card = dev_get_drvdata(dev);
760 if (!card)
761 return -EINVAL;
763 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
766 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
767 struct device_attribute *attr, const char *buf, size_t count)
769 struct qeth_card *card = dev_get_drvdata(dev);
771 if (!card)
772 return -EINVAL;
774 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
777 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
778 qeth_l3_dev_ipato_add6_show,
779 qeth_l3_dev_ipato_add6_store);
781 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
782 struct device_attribute *attr, const char *buf, size_t count)
784 struct qeth_card *card = dev_get_drvdata(dev);
786 if (!card)
787 return -EINVAL;
789 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
792 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
793 qeth_l3_dev_ipato_del6_store);
795 static struct attribute *qeth_ipato_device_attrs[] = {
796 &dev_attr_ipato_enable.attr,
797 &dev_attr_ipato_invert4.attr,
798 &dev_attr_ipato_add4.attr,
799 &dev_attr_ipato_del4.attr,
800 &dev_attr_ipato_invert6.attr,
801 &dev_attr_ipato_add6.attr,
802 &dev_attr_ipato_del6.attr,
803 NULL,
806 static struct attribute_group qeth_device_ipato_group = {
807 .name = "ipa_takeover",
808 .attrs = qeth_ipato_device_attrs,
811 static ssize_t qeth_l3_dev_vipa_add_show(char *buf, struct qeth_card *card,
812 enum qeth_prot_versions proto)
814 struct qeth_ipaddr *ipaddr;
815 char addr_str[40];
816 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
817 unsigned long flags;
818 int i = 0;
820 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
821 entry_len += 2; /* \n + terminator */
822 spin_lock_irqsave(&card->ip_lock, flags);
823 list_for_each_entry(ipaddr, &card->ip_list, entry) {
824 if (ipaddr->proto != proto)
825 continue;
826 if (ipaddr->type != QETH_IP_TYPE_VIPA)
827 continue;
828 /* String must not be longer than PAGE_SIZE. So we check if
829 * string length gets near PAGE_SIZE. Then we can savely display
830 * the next IPv6 address (worst case, compared to IPv4) */
831 if ((PAGE_SIZE - i) <= entry_len)
832 break;
833 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
834 addr_str);
835 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
837 spin_unlock_irqrestore(&card->ip_lock, flags);
838 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
840 return i;
843 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
844 struct device_attribute *attr, char *buf)
846 struct qeth_card *card = dev_get_drvdata(dev);
848 if (!card)
849 return -EINVAL;
851 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
854 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
855 u8 *addr)
857 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
858 return -EINVAL;
860 return 0;
863 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
864 struct qeth_card *card, enum qeth_prot_versions proto)
866 u8 addr[16] = {0, };
867 int rc;
869 mutex_lock(&card->conf_mutex);
870 rc = qeth_l3_parse_vipae(buf, proto, addr);
871 if (!rc)
872 rc = qeth_l3_add_vipa(card, proto, addr);
873 mutex_unlock(&card->conf_mutex);
874 return rc ? rc : count;
877 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
878 struct device_attribute *attr, const char *buf, size_t count)
880 struct qeth_card *card = dev_get_drvdata(dev);
882 if (!card)
883 return -EINVAL;
885 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
888 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
889 qeth_l3_dev_vipa_add4_show,
890 qeth_l3_dev_vipa_add4_store);
892 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
893 struct qeth_card *card, enum qeth_prot_versions proto)
895 u8 addr[16];
896 int rc;
898 mutex_lock(&card->conf_mutex);
899 rc = qeth_l3_parse_vipae(buf, proto, addr);
900 if (!rc)
901 qeth_l3_del_vipa(card, proto, addr);
902 mutex_unlock(&card->conf_mutex);
903 return rc ? rc : count;
906 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
907 struct device_attribute *attr, const char *buf, size_t count)
909 struct qeth_card *card = dev_get_drvdata(dev);
911 if (!card)
912 return -EINVAL;
914 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
917 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
918 qeth_l3_dev_vipa_del4_store);
920 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
921 struct device_attribute *attr, char *buf)
923 struct qeth_card *card = dev_get_drvdata(dev);
925 if (!card)
926 return -EINVAL;
928 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
931 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
932 struct device_attribute *attr, const char *buf, size_t count)
934 struct qeth_card *card = dev_get_drvdata(dev);
936 if (!card)
937 return -EINVAL;
939 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
942 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
943 qeth_l3_dev_vipa_add6_show,
944 qeth_l3_dev_vipa_add6_store);
946 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
947 struct device_attribute *attr, const char *buf, size_t count)
949 struct qeth_card *card = dev_get_drvdata(dev);
951 if (!card)
952 return -EINVAL;
954 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
957 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
958 qeth_l3_dev_vipa_del6_store);
960 static struct attribute *qeth_vipa_device_attrs[] = {
961 &dev_attr_vipa_add4.attr,
962 &dev_attr_vipa_del4.attr,
963 &dev_attr_vipa_add6.attr,
964 &dev_attr_vipa_del6.attr,
965 NULL,
968 static struct attribute_group qeth_device_vipa_group = {
969 .name = "vipa",
970 .attrs = qeth_vipa_device_attrs,
973 static ssize_t qeth_l3_dev_rxip_add_show(char *buf, struct qeth_card *card,
974 enum qeth_prot_versions proto)
976 struct qeth_ipaddr *ipaddr;
977 char addr_str[40];
978 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
979 unsigned long flags;
980 int i = 0;
982 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
983 entry_len += 2; /* \n + terminator */
984 spin_lock_irqsave(&card->ip_lock, flags);
985 list_for_each_entry(ipaddr, &card->ip_list, entry) {
986 if (ipaddr->proto != proto)
987 continue;
988 if (ipaddr->type != QETH_IP_TYPE_RXIP)
989 continue;
990 /* String must not be longer than PAGE_SIZE. So we check if
991 * string length gets near PAGE_SIZE. Then we can savely display
992 * the next IPv6 address (worst case, compared to IPv4) */
993 if ((PAGE_SIZE - i) <= entry_len)
994 break;
995 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
996 addr_str);
997 i += snprintf(buf + i, PAGE_SIZE - i, "%s\n", addr_str);
999 spin_unlock_irqrestore(&card->ip_lock, flags);
1000 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
1002 return i;
1005 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
1006 struct device_attribute *attr, char *buf)
1008 struct qeth_card *card = dev_get_drvdata(dev);
1010 if (!card)
1011 return -EINVAL;
1013 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
1016 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
1017 u8 *addr)
1019 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
1020 return -EINVAL;
1022 return 0;
1025 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
1026 struct qeth_card *card, enum qeth_prot_versions proto)
1028 u8 addr[16] = {0, };
1029 int rc;
1031 mutex_lock(&card->conf_mutex);
1032 rc = qeth_l3_parse_rxipe(buf, proto, addr);
1033 if (!rc)
1034 rc = qeth_l3_add_rxip(card, proto, addr);
1035 mutex_unlock(&card->conf_mutex);
1036 return rc ? rc : count;
1039 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
1040 struct device_attribute *attr, const char *buf, size_t count)
1042 struct qeth_card *card = dev_get_drvdata(dev);
1044 if (!card)
1045 return -EINVAL;
1047 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
1050 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
1051 qeth_l3_dev_rxip_add4_show,
1052 qeth_l3_dev_rxip_add4_store);
1054 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
1055 struct qeth_card *card, enum qeth_prot_versions proto)
1057 u8 addr[16];
1058 int rc;
1060 mutex_lock(&card->conf_mutex);
1061 rc = qeth_l3_parse_rxipe(buf, proto, addr);
1062 if (!rc)
1063 qeth_l3_del_rxip(card, proto, addr);
1064 mutex_unlock(&card->conf_mutex);
1065 return rc ? rc : count;
1068 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
1069 struct device_attribute *attr, const char *buf, size_t count)
1071 struct qeth_card *card = dev_get_drvdata(dev);
1073 if (!card)
1074 return -EINVAL;
1076 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
1079 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
1080 qeth_l3_dev_rxip_del4_store);
1082 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
1083 struct device_attribute *attr, char *buf)
1085 struct qeth_card *card = dev_get_drvdata(dev);
1087 if (!card)
1088 return -EINVAL;
1090 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
1093 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
1094 struct device_attribute *attr, const char *buf, size_t count)
1096 struct qeth_card *card = dev_get_drvdata(dev);
1098 if (!card)
1099 return -EINVAL;
1101 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
1104 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
1105 qeth_l3_dev_rxip_add6_show,
1106 qeth_l3_dev_rxip_add6_store);
1108 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
1109 struct device_attribute *attr, const char *buf, size_t count)
1111 struct qeth_card *card = dev_get_drvdata(dev);
1113 if (!card)
1114 return -EINVAL;
1116 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
1119 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
1120 qeth_l3_dev_rxip_del6_store);
1122 static struct attribute *qeth_rxip_device_attrs[] = {
1123 &dev_attr_rxip_add4.attr,
1124 &dev_attr_rxip_del4.attr,
1125 &dev_attr_rxip_add6.attr,
1126 &dev_attr_rxip_del6.attr,
1127 NULL,
1130 static struct attribute_group qeth_device_rxip_group = {
1131 .name = "rxip",
1132 .attrs = qeth_rxip_device_attrs,
1135 int qeth_l3_create_device_attributes(struct device *dev)
1137 int ret;
1139 ret = sysfs_create_group(&dev->kobj, &qeth_l3_device_attr_group);
1140 if (ret)
1141 return ret;
1143 ret = sysfs_create_group(&dev->kobj, &qeth_device_ipato_group);
1144 if (ret) {
1145 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1146 return ret;
1149 ret = sysfs_create_group(&dev->kobj, &qeth_device_vipa_group);
1150 if (ret) {
1151 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1152 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1153 return ret;
1156 ret = sysfs_create_group(&dev->kobj, &qeth_device_rxip_group);
1157 if (ret) {
1158 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1159 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1160 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1161 return ret;
1163 return 0;
1166 void qeth_l3_remove_device_attributes(struct device *dev)
1168 sysfs_remove_group(&dev->kobj, &qeth_l3_device_attr_group);
1169 sysfs_remove_group(&dev->kobj, &qeth_device_ipato_group);
1170 sysfs_remove_group(&dev->kobj, &qeth_device_vipa_group);
1171 sysfs_remove_group(&dev->kobj, &qeth_device_rxip_group);