]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/s390/net/qeth_l3_sys.c
s390/qeth: lock IP table while applying takeover changes
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / net / qeth_l3_sys.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2007
4 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5 * Frank Pavlic <fpavlic@de.ibm.com>,
6 * Thomas Spatzier <tspat@de.ibm.com>,
7 * Frank Blaschka <frank.blaschka@de.ibm.com>
8 */
9
10 #include <linux/slab.h>
11 #include <asm/ebcdic.h>
12 #include <linux/hashtable.h>
13 #include "qeth_l3.h"
14
15 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \
16 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store)
17
18 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card,
19 struct qeth_routing_info *route, char *buf)
20 {
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");
43 }
44 }
45
46 static ssize_t qeth_l3_dev_route4_show(struct device *dev,
47 struct device_attribute *attr, char *buf)
48 {
49 struct qeth_card *card = dev_get_drvdata(dev);
50
51 if (!card)
52 return -EINVAL;
53
54 return qeth_l3_dev_route_show(card, &card->options.route4, buf);
55 }
56
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)
60 {
61 enum qeth_routing_types old_route_type = route->type;
62 int rc = 0;
63
64 mutex_lock(&card->conf_mutex);
65 if (sysfs_streq(buf, "no_router")) {
66 route->type = NO_ROUTER;
67 } else if (sysfs_streq(buf, "primary_connector")) {
68 route->type = PRIMARY_CONNECTOR;
69 } else if (sysfs_streq(buf, "secondary_connector")) {
70 route->type = SECONDARY_CONNECTOR;
71 } else if (sysfs_streq(buf, "primary_router")) {
72 route->type = PRIMARY_ROUTER;
73 } else if (sysfs_streq(buf, "secondary_router")) {
74 route->type = SECONDARY_ROUTER;
75 } else if (sysfs_streq(buf, "multicast_router")) {
76 route->type = MULTICAST_ROUTER;
77 } else {
78 rc = -EINVAL;
79 goto out;
80 }
81 if (qeth_card_hw_is_reachable(card) &&
82 (old_route_type != route->type)) {
83 if (prot == QETH_PROT_IPV4)
84 rc = qeth_l3_setrouting_v4(card);
85 else if (prot == QETH_PROT_IPV6)
86 rc = qeth_l3_setrouting_v6(card);
87 }
88 out:
89 if (rc)
90 route->type = old_route_type;
91 mutex_unlock(&card->conf_mutex);
92 return rc ? rc : count;
93 }
94
95 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
96 struct device_attribute *attr, const char *buf, size_t count)
97 {
98 struct qeth_card *card = dev_get_drvdata(dev);
99
100 if (!card)
101 return -EINVAL;
102
103 return qeth_l3_dev_route_store(card, &card->options.route4,
104 QETH_PROT_IPV4, buf, count);
105 }
106
107 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
108 qeth_l3_dev_route4_store);
109
110 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
112 {
113 struct qeth_card *card = dev_get_drvdata(dev);
114
115 if (!card)
116 return -EINVAL;
117
118 return qeth_l3_dev_route_show(card, &card->options.route6, buf);
119 }
120
121 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
122 struct device_attribute *attr, const char *buf, size_t count)
123 {
124 struct qeth_card *card = dev_get_drvdata(dev);
125
126 if (!card)
127 return -EINVAL;
128
129 return qeth_l3_dev_route_store(card, &card->options.route6,
130 QETH_PROT_IPV6, buf, count);
131 }
132
133 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
134 qeth_l3_dev_route6_store);
135
136 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138 {
139 struct qeth_card *card = dev_get_drvdata(dev);
140
141 if (!card)
142 return -EINVAL;
143
144 return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
145 }
146
147 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
148 struct device_attribute *attr, const char *buf, size_t count)
149 {
150 struct qeth_card *card = dev_get_drvdata(dev);
151 char *tmp;
152 int i, rc = 0;
153
154 if (!card)
155 return -EINVAL;
156
157 mutex_lock(&card->conf_mutex);
158 if ((card->state != CARD_STATE_DOWN) &&
159 (card->state != CARD_STATE_RECOVER)) {
160 rc = -EPERM;
161 goto out;
162 }
163
164 i = simple_strtoul(buf, &tmp, 16);
165 if ((i == 0) || (i == 1))
166 card->options.fake_broadcast = i;
167 else
168 rc = -EINVAL;
169 out:
170 mutex_unlock(&card->conf_mutex);
171 return rc ? rc : count;
172 }
173
174 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
175 qeth_l3_dev_fake_broadcast_store);
176
177 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179 {
180 struct qeth_card *card = dev_get_drvdata(dev);
181
182 if (!card)
183 return -EINVAL;
184
185 return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
186 }
187
188 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
189 struct device_attribute *attr, const char *buf, size_t count)
190 {
191 struct qeth_card *card = dev_get_drvdata(dev);
192 int rc = 0;
193 unsigned long i;
194
195 if (!card)
196 return -EINVAL;
197
198 if (card->info.type != QETH_CARD_TYPE_IQD)
199 return -EPERM;
200 if (card->options.cq == QETH_CQ_ENABLED)
201 return -EPERM;
202
203 mutex_lock(&card->conf_mutex);
204 if ((card->state != CARD_STATE_DOWN) &&
205 (card->state != CARD_STATE_RECOVER)) {
206 rc = -EPERM;
207 goto out;
208 }
209
210 rc = kstrtoul(buf, 16, &i);
211 if (rc) {
212 rc = -EINVAL;
213 goto out;
214 }
215 switch (i) {
216 case 0:
217 card->options.sniffer = i;
218 break;
219 case 1:
220 qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
221 if (card->ssqd.qdioac2 & QETH_SNIFF_AVAIL) {
222 card->options.sniffer = i;
223 if (card->qdio.init_pool.buf_count !=
224 QETH_IN_BUF_COUNT_MAX)
225 qeth_realloc_buffer_pool(card,
226 QETH_IN_BUF_COUNT_MAX);
227 } else
228 rc = -EPERM;
229 break;
230 default:
231 rc = -EINVAL;
232 }
233 out:
234 mutex_unlock(&card->conf_mutex);
235 return rc ? rc : count;
236 }
237
238 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
239 qeth_l3_dev_sniffer_store);
240
241
242 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244 {
245 struct qeth_card *card = dev_get_drvdata(dev);
246 char tmp_hsuid[9];
247
248 if (!card)
249 return -EINVAL;
250
251 if (card->info.type != QETH_CARD_TYPE_IQD)
252 return -EPERM;
253
254 memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
255 EBCASC(tmp_hsuid, 8);
256 return sprintf(buf, "%s\n", tmp_hsuid);
257 }
258
259 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
260 struct device_attribute *attr, const char *buf, size_t count)
261 {
262 struct qeth_card *card = dev_get_drvdata(dev);
263 struct qeth_ipaddr *addr;
264 char *tmp;
265 int i;
266
267 if (!card)
268 return -EINVAL;
269
270 if (card->info.type != QETH_CARD_TYPE_IQD)
271 return -EPERM;
272 if (card->state != CARD_STATE_DOWN &&
273 card->state != CARD_STATE_RECOVER)
274 return -EPERM;
275 if (card->options.sniffer)
276 return -EPERM;
277 if (card->options.cq == QETH_CQ_NOTAVAILABLE)
278 return -EPERM;
279
280 tmp = strsep((char **)&buf, "\n");
281 if (strlen(tmp) > 8)
282 return -EINVAL;
283
284 if (card->options.hsuid[0]) {
285 /* delete old ip address */
286 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
287 if (!addr)
288 return -ENOMEM;
289
290 addr->u.a6.addr.s6_addr32[0] = cpu_to_be32(0xfe800000);
291 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
292 for (i = 8; i < 16; i++)
293 addr->u.a6.addr.s6_addr[i] =
294 card->options.hsuid[i - 8];
295 addr->u.a6.pfxlen = 0;
296 addr->type = QETH_IP_TYPE_NORMAL;
297
298 spin_lock_bh(&card->ip_lock);
299 qeth_l3_delete_ip(card, addr);
300 spin_unlock_bh(&card->ip_lock);
301 kfree(addr);
302 }
303
304 if (strlen(tmp) == 0) {
305 /* delete ip address only */
306 card->options.hsuid[0] = '\0';
307 if (card->dev)
308 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
309 qeth_configure_cq(card, QETH_CQ_DISABLED);
310 return count;
311 }
312
313 if (qeth_configure_cq(card, QETH_CQ_ENABLED))
314 return -EPERM;
315
316 snprintf(card->options.hsuid, sizeof(card->options.hsuid),
317 "%-8s", tmp);
318 ASCEBC(card->options.hsuid, 8);
319 if (card->dev)
320 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
321
322 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
323 if (addr != NULL) {
324 addr->u.a6.addr.s6_addr32[0] = cpu_to_be32(0xfe800000);
325 addr->u.a6.addr.s6_addr32[1] = 0x00000000;
326 for (i = 8; i < 16; i++)
327 addr->u.a6.addr.s6_addr[i] = card->options.hsuid[i - 8];
328 addr->u.a6.pfxlen = 0;
329 addr->type = QETH_IP_TYPE_NORMAL;
330 } else
331 return -ENOMEM;
332
333 spin_lock_bh(&card->ip_lock);
334 qeth_l3_add_ip(card, addr);
335 spin_unlock_bh(&card->ip_lock);
336 kfree(addr);
337
338 return count;
339 }
340
341 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
342 qeth_l3_dev_hsuid_store);
343
344
345 static struct attribute *qeth_l3_device_attrs[] = {
346 &dev_attr_route4.attr,
347 &dev_attr_route6.attr,
348 &dev_attr_fake_broadcast.attr,
349 &dev_attr_sniffer.attr,
350 &dev_attr_hsuid.attr,
351 NULL,
352 };
353
354 static const struct attribute_group qeth_l3_device_attr_group = {
355 .attrs = qeth_l3_device_attrs,
356 };
357
358 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
360 {
361 struct qeth_card *card = dev_get_drvdata(dev);
362
363 if (!card)
364 return -EINVAL;
365
366 return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
367 }
368
369 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
370 struct device_attribute *attr, const char *buf, size_t count)
371 {
372 struct qeth_card *card = dev_get_drvdata(dev);
373 struct qeth_ipaddr *addr;
374 int i, rc = 0;
375 bool enable;
376
377 if (!card)
378 return -EINVAL;
379
380 mutex_lock(&card->conf_mutex);
381 if ((card->state != CARD_STATE_DOWN) &&
382 (card->state != CARD_STATE_RECOVER)) {
383 rc = -EPERM;
384 goto out;
385 }
386
387 if (sysfs_streq(buf, "toggle")) {
388 enable = !card->ipato.enabled;
389 } else if (kstrtobool(buf, &enable)) {
390 rc = -EINVAL;
391 goto out;
392 }
393
394 if (card->ipato.enabled == enable)
395 goto out;
396 card->ipato.enabled = enable;
397
398 spin_lock_bh(&card->ip_lock);
399 hash_for_each(card->ip_htable, i, addr, hnode) {
400 if (addr->type != QETH_IP_TYPE_NORMAL)
401 continue;
402 if (!enable)
403 addr->set_flags &= ~QETH_IPA_SETIP_TAKEOVER_FLAG;
404 else if (qeth_l3_is_addr_covered_by_ipato(card, addr))
405 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
406 }
407 spin_unlock_bh(&card->ip_lock);
408 out:
409 mutex_unlock(&card->conf_mutex);
410 return rc ? rc : count;
411 }
412
413 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
414 qeth_l3_dev_ipato_enable_show,
415 qeth_l3_dev_ipato_enable_store);
416
417 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
419 {
420 struct qeth_card *card = dev_get_drvdata(dev);
421
422 if (!card)
423 return -EINVAL;
424
425 return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
426 }
427
428 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
429 struct device_attribute *attr,
430 const char *buf, size_t count)
431 {
432 struct qeth_card *card = dev_get_drvdata(dev);
433 int rc = 0;
434
435 if (!card)
436 return -EINVAL;
437
438 mutex_lock(&card->conf_mutex);
439 if (sysfs_streq(buf, "toggle"))
440 card->ipato.invert4 = (card->ipato.invert4)? 0 : 1;
441 else if (sysfs_streq(buf, "1"))
442 card->ipato.invert4 = 1;
443 else if (sysfs_streq(buf, "0"))
444 card->ipato.invert4 = 0;
445 else
446 rc = -EINVAL;
447 mutex_unlock(&card->conf_mutex);
448 return rc ? rc : count;
449 }
450
451 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
452 qeth_l3_dev_ipato_invert4_show,
453 qeth_l3_dev_ipato_invert4_store);
454
455 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
456 enum qeth_prot_versions proto)
457 {
458 struct qeth_ipato_entry *ipatoe;
459 char addr_str[40];
460 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
461 int i = 0;
462
463 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
464 /* add strlen for "/<mask>\n" */
465 entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
466 spin_lock_bh(&card->ip_lock);
467 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
468 if (ipatoe->proto != proto)
469 continue;
470 /* String must not be longer than PAGE_SIZE. So we check if
471 * string length gets near PAGE_SIZE. Then we can savely display
472 * the next IPv6 address (worst case, compared to IPv4) */
473 if ((PAGE_SIZE - i) <= entry_len)
474 break;
475 qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
476 i += snprintf(buf + i, PAGE_SIZE - i,
477 "%s/%i\n", addr_str, ipatoe->mask_bits);
478 }
479 spin_unlock_bh(&card->ip_lock);
480 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
481
482 return i;
483 }
484
485 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487 {
488 struct qeth_card *card = dev_get_drvdata(dev);
489
490 if (!card)
491 return -EINVAL;
492
493 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
494 }
495
496 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
497 u8 *addr, int *mask_bits)
498 {
499 const char *start, *end;
500 char *tmp;
501 char buffer[40] = {0, };
502
503 start = buf;
504 /* get address string */
505 end = strchr(start, '/');
506 if (!end || (end - start >= 40)) {
507 return -EINVAL;
508 }
509 strncpy(buffer, start, end - start);
510 if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
511 return -EINVAL;
512 }
513 start = end + 1;
514 *mask_bits = simple_strtoul(start, &tmp, 10);
515 if (!strlen(start) ||
516 (tmp == start) ||
517 (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
518 return -EINVAL;
519 }
520 return 0;
521 }
522
523 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
524 struct qeth_card *card, enum qeth_prot_versions proto)
525 {
526 struct qeth_ipato_entry *ipatoe;
527 u8 addr[16];
528 int mask_bits;
529 int rc = 0;
530
531 mutex_lock(&card->conf_mutex);
532 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
533 if (rc)
534 goto out;
535
536 ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
537 if (!ipatoe) {
538 rc = -ENOMEM;
539 goto out;
540 }
541 ipatoe->proto = proto;
542 memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
543 ipatoe->mask_bits = mask_bits;
544
545 rc = qeth_l3_add_ipato_entry(card, ipatoe);
546 if (rc)
547 kfree(ipatoe);
548 out:
549 mutex_unlock(&card->conf_mutex);
550 return rc ? rc : count;
551 }
552
553 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
554 struct device_attribute *attr, const char *buf, size_t count)
555 {
556 struct qeth_card *card = dev_get_drvdata(dev);
557
558 if (!card)
559 return -EINVAL;
560
561 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
562 }
563
564 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
565 qeth_l3_dev_ipato_add4_show,
566 qeth_l3_dev_ipato_add4_store);
567
568 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
569 struct qeth_card *card, enum qeth_prot_versions proto)
570 {
571 u8 addr[16];
572 int mask_bits;
573 int rc = 0;
574
575 mutex_lock(&card->conf_mutex);
576 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
577 if (!rc)
578 qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
579 mutex_unlock(&card->conf_mutex);
580 return rc ? rc : count;
581 }
582
583 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
584 struct device_attribute *attr, const char *buf, size_t count)
585 {
586 struct qeth_card *card = dev_get_drvdata(dev);
587
588 if (!card)
589 return -EINVAL;
590
591 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
592 }
593
594 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
595 qeth_l3_dev_ipato_del4_store);
596
597 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
599 {
600 struct qeth_card *card = dev_get_drvdata(dev);
601
602 if (!card)
603 return -EINVAL;
604
605 return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
606 }
607
608 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
609 struct device_attribute *attr, const char *buf, size_t count)
610 {
611 struct qeth_card *card = dev_get_drvdata(dev);
612 int rc = 0;
613
614 if (!card)
615 return -EINVAL;
616
617 mutex_lock(&card->conf_mutex);
618 if (sysfs_streq(buf, "toggle"))
619 card->ipato.invert6 = (card->ipato.invert6)? 0 : 1;
620 else if (sysfs_streq(buf, "1"))
621 card->ipato.invert6 = 1;
622 else if (sysfs_streq(buf, "0"))
623 card->ipato.invert6 = 0;
624 else
625 rc = -EINVAL;
626 mutex_unlock(&card->conf_mutex);
627 return rc ? rc : count;
628 }
629
630 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
631 qeth_l3_dev_ipato_invert6_show,
632 qeth_l3_dev_ipato_invert6_store);
633
634
635 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
636 struct device_attribute *attr, char *buf)
637 {
638 struct qeth_card *card = dev_get_drvdata(dev);
639
640 if (!card)
641 return -EINVAL;
642
643 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
644 }
645
646 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
647 struct device_attribute *attr, const char *buf, size_t count)
648 {
649 struct qeth_card *card = dev_get_drvdata(dev);
650
651 if (!card)
652 return -EINVAL;
653
654 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
655 }
656
657 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
658 qeth_l3_dev_ipato_add6_show,
659 qeth_l3_dev_ipato_add6_store);
660
661 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
662 struct device_attribute *attr, const char *buf, size_t count)
663 {
664 struct qeth_card *card = dev_get_drvdata(dev);
665
666 if (!card)
667 return -EINVAL;
668
669 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
670 }
671
672 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
673 qeth_l3_dev_ipato_del6_store);
674
675 static struct attribute *qeth_ipato_device_attrs[] = {
676 &dev_attr_ipato_enable.attr,
677 &dev_attr_ipato_invert4.attr,
678 &dev_attr_ipato_add4.attr,
679 &dev_attr_ipato_del4.attr,
680 &dev_attr_ipato_invert6.attr,
681 &dev_attr_ipato_add6.attr,
682 &dev_attr_ipato_del6.attr,
683 NULL,
684 };
685
686 static const struct attribute_group qeth_device_ipato_group = {
687 .name = "ipa_takeover",
688 .attrs = qeth_ipato_device_attrs,
689 };
690
691 static ssize_t qeth_l3_dev_vipa_add_show(char *buf, struct qeth_card *card,
692 enum qeth_prot_versions proto)
693 {
694 struct qeth_ipaddr *ipaddr;
695 char addr_str[40];
696 int str_len = 0;
697 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
698 int i;
699
700 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
701 entry_len += 2; /* \n + terminator */
702 spin_lock_bh(&card->ip_lock);
703 hash_for_each(card->ip_htable, i, ipaddr, hnode) {
704 if (ipaddr->proto != proto)
705 continue;
706 if (ipaddr->type != QETH_IP_TYPE_VIPA)
707 continue;
708 /* String must not be longer than PAGE_SIZE. So we check if
709 * string length gets near PAGE_SIZE. Then we can savely display
710 * the next IPv6 address (worst case, compared to IPv4) */
711 if ((PAGE_SIZE - str_len) <= entry_len)
712 break;
713 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
714 addr_str);
715 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n",
716 addr_str);
717 }
718 spin_unlock_bh(&card->ip_lock);
719 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
720
721 return str_len;
722 }
723
724 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
725 struct device_attribute *attr, char *buf)
726 {
727 struct qeth_card *card = dev_get_drvdata(dev);
728
729 if (!card)
730 return -EINVAL;
731
732 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV4);
733 }
734
735 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
736 u8 *addr)
737 {
738 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
739 return -EINVAL;
740 }
741 return 0;
742 }
743
744 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
745 struct qeth_card *card, enum qeth_prot_versions proto)
746 {
747 u8 addr[16] = {0, };
748 int rc;
749
750 mutex_lock(&card->conf_mutex);
751 rc = qeth_l3_parse_vipae(buf, proto, addr);
752 if (!rc)
753 rc = qeth_l3_add_vipa(card, proto, addr);
754 mutex_unlock(&card->conf_mutex);
755 return rc ? rc : count;
756 }
757
758 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
759 struct device_attribute *attr, const char *buf, size_t count)
760 {
761 struct qeth_card *card = dev_get_drvdata(dev);
762
763 if (!card)
764 return -EINVAL;
765
766 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
767 }
768
769 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
770 qeth_l3_dev_vipa_add4_show,
771 qeth_l3_dev_vipa_add4_store);
772
773 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
774 struct qeth_card *card, enum qeth_prot_versions proto)
775 {
776 u8 addr[16];
777 int rc;
778
779 mutex_lock(&card->conf_mutex);
780 rc = qeth_l3_parse_vipae(buf, proto, addr);
781 if (!rc)
782 qeth_l3_del_vipa(card, proto, addr);
783 mutex_unlock(&card->conf_mutex);
784 return rc ? rc : count;
785 }
786
787 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
788 struct device_attribute *attr, const char *buf, size_t count)
789 {
790 struct qeth_card *card = dev_get_drvdata(dev);
791
792 if (!card)
793 return -EINVAL;
794
795 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
796 }
797
798 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
799 qeth_l3_dev_vipa_del4_store);
800
801 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
803 {
804 struct qeth_card *card = dev_get_drvdata(dev);
805
806 if (!card)
807 return -EINVAL;
808
809 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV6);
810 }
811
812 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
813 struct device_attribute *attr, const char *buf, size_t count)
814 {
815 struct qeth_card *card = dev_get_drvdata(dev);
816
817 if (!card)
818 return -EINVAL;
819
820 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
821 }
822
823 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
824 qeth_l3_dev_vipa_add6_show,
825 qeth_l3_dev_vipa_add6_store);
826
827 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
828 struct device_attribute *attr, const char *buf, size_t count)
829 {
830 struct qeth_card *card = dev_get_drvdata(dev);
831
832 if (!card)
833 return -EINVAL;
834
835 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
836 }
837
838 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
839 qeth_l3_dev_vipa_del6_store);
840
841 static struct attribute *qeth_vipa_device_attrs[] = {
842 &dev_attr_vipa_add4.attr,
843 &dev_attr_vipa_del4.attr,
844 &dev_attr_vipa_add6.attr,
845 &dev_attr_vipa_del6.attr,
846 NULL,
847 };
848
849 static const struct attribute_group qeth_device_vipa_group = {
850 .name = "vipa",
851 .attrs = qeth_vipa_device_attrs,
852 };
853
854 static ssize_t qeth_l3_dev_rxip_add_show(char *buf, struct qeth_card *card,
855 enum qeth_prot_versions proto)
856 {
857 struct qeth_ipaddr *ipaddr;
858 char addr_str[40];
859 int str_len = 0;
860 int entry_len; /* length of 1 entry string, differs between v4 and v6 */
861 int i;
862
863 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
864 entry_len += 2; /* \n + terminator */
865 spin_lock_bh(&card->ip_lock);
866 hash_for_each(card->ip_htable, i, ipaddr, hnode) {
867 if (ipaddr->proto != proto)
868 continue;
869 if (ipaddr->type != QETH_IP_TYPE_RXIP)
870 continue;
871 /* String must not be longer than PAGE_SIZE. So we check if
872 * string length gets near PAGE_SIZE. Then we can savely display
873 * the next IPv6 address (worst case, compared to IPv4) */
874 if ((PAGE_SIZE - str_len) <= entry_len)
875 break;
876 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
877 addr_str);
878 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n",
879 addr_str);
880 }
881 spin_unlock_bh(&card->ip_lock);
882 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
883
884 return str_len;
885 }
886
887 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
888 struct device_attribute *attr, char *buf)
889 {
890 struct qeth_card *card = dev_get_drvdata(dev);
891
892 if (!card)
893 return -EINVAL;
894
895 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV4);
896 }
897
898 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
899 u8 *addr)
900 {
901 __be32 ipv4_addr;
902 struct in6_addr ipv6_addr;
903
904 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
905 return -EINVAL;
906 }
907 if (proto == QETH_PROT_IPV4) {
908 memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
909 if (ipv4_is_multicast(ipv4_addr)) {
910 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
911 return -EINVAL;
912 }
913 } else if (proto == QETH_PROT_IPV6) {
914 memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
915 if (ipv6_addr_is_multicast(&ipv6_addr)) {
916 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
917 return -EINVAL;
918 }
919 }
920
921 return 0;
922 }
923
924 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
925 struct qeth_card *card, enum qeth_prot_versions proto)
926 {
927 u8 addr[16] = {0, };
928 int rc;
929
930 mutex_lock(&card->conf_mutex);
931 rc = qeth_l3_parse_rxipe(buf, proto, addr);
932 if (!rc)
933 rc = qeth_l3_add_rxip(card, proto, addr);
934 mutex_unlock(&card->conf_mutex);
935 return rc ? rc : count;
936 }
937
938 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
939 struct device_attribute *attr, const char *buf, size_t count)
940 {
941 struct qeth_card *card = dev_get_drvdata(dev);
942
943 if (!card)
944 return -EINVAL;
945
946 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
947 }
948
949 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
950 qeth_l3_dev_rxip_add4_show,
951 qeth_l3_dev_rxip_add4_store);
952
953 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
954 struct qeth_card *card, enum qeth_prot_versions proto)
955 {
956 u8 addr[16];
957 int rc;
958
959 mutex_lock(&card->conf_mutex);
960 rc = qeth_l3_parse_rxipe(buf, proto, addr);
961 if (!rc)
962 qeth_l3_del_rxip(card, proto, addr);
963 mutex_unlock(&card->conf_mutex);
964 return rc ? rc : count;
965 }
966
967 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
968 struct device_attribute *attr, const char *buf, size_t count)
969 {
970 struct qeth_card *card = dev_get_drvdata(dev);
971
972 if (!card)
973 return -EINVAL;
974
975 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
976 }
977
978 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
979 qeth_l3_dev_rxip_del4_store);
980
981 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
982 struct device_attribute *attr, char *buf)
983 {
984 struct qeth_card *card = dev_get_drvdata(dev);
985
986 if (!card)
987 return -EINVAL;
988
989 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV6);
990 }
991
992 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
993 struct device_attribute *attr, const char *buf, size_t count)
994 {
995 struct qeth_card *card = dev_get_drvdata(dev);
996
997 if (!card)
998 return -EINVAL;
999
1000 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
1001 }
1002
1003 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
1004 qeth_l3_dev_rxip_add6_show,
1005 qeth_l3_dev_rxip_add6_store);
1006
1007 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
1008 struct device_attribute *attr, const char *buf, size_t count)
1009 {
1010 struct qeth_card *card = dev_get_drvdata(dev);
1011
1012 if (!card)
1013 return -EINVAL;
1014
1015 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
1016 }
1017
1018 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
1019 qeth_l3_dev_rxip_del6_store);
1020
1021 static struct attribute *qeth_rxip_device_attrs[] = {
1022 &dev_attr_rxip_add4.attr,
1023 &dev_attr_rxip_del4.attr,
1024 &dev_attr_rxip_add6.attr,
1025 &dev_attr_rxip_del6.attr,
1026 NULL,
1027 };
1028
1029 static const struct attribute_group qeth_device_rxip_group = {
1030 .name = "rxip",
1031 .attrs = qeth_rxip_device_attrs,
1032 };
1033
1034 static const struct attribute_group *qeth_l3_only_attr_groups[] = {
1035 &qeth_l3_device_attr_group,
1036 &qeth_device_ipato_group,
1037 &qeth_device_vipa_group,
1038 &qeth_device_rxip_group,
1039 NULL,
1040 };
1041
1042 int qeth_l3_create_device_attributes(struct device *dev)
1043 {
1044 return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
1045 }
1046
1047 void qeth_l3_remove_device_attributes(struct device *dev)
1048 {
1049 sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
1050 }
1051
1052 const struct attribute_group *qeth_l3_attr_groups[] = {
1053 &qeth_device_attr_group,
1054 &qeth_device_blkt_group,
1055 /* l3 specific, see qeth_l3_only_attr_groups: */
1056 &qeth_l3_device_attr_group,
1057 &qeth_device_ipato_group,
1058 &qeth_device_vipa_group,
1059 &qeth_device_rxip_group,
1060 NULL,
1061 };