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