qeth_l3_sys.c 25 KB

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