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