qeth_l3_sys.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. memcpy(card->dev->perm_addr, card->options.hsuid, 9);
  254. qeth_configure_cq(card, QETH_CQ_DISABLED);
  255. return count;
  256. }
  257. if (qeth_configure_cq(card, QETH_CQ_ENABLED))
  258. return -EPERM;
  259. snprintf(card->options.hsuid, sizeof(card->options.hsuid),
  260. "%-8s", tmp);
  261. ASCEBC(card->options.hsuid, 8);
  262. memcpy(card->dev->perm_addr, card->options.hsuid, 9);
  263. rc = qeth_l3_modify_hsuid(card, true);
  264. return rc ? rc : count;
  265. }
  266. static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
  267. qeth_l3_dev_hsuid_store);
  268. static struct attribute *qeth_l3_device_attrs[] = {
  269. &dev_attr_route4.attr,
  270. &dev_attr_route6.attr,
  271. &dev_attr_fake_broadcast.attr,
  272. &dev_attr_sniffer.attr,
  273. &dev_attr_hsuid.attr,
  274. NULL,
  275. };
  276. static const struct attribute_group qeth_l3_device_attr_group = {
  277. .attrs = qeth_l3_device_attrs,
  278. };
  279. static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
  280. struct device_attribute *attr, char *buf)
  281. {
  282. struct qeth_card *card = dev_get_drvdata(dev);
  283. if (!card)
  284. return -EINVAL;
  285. return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
  286. }
  287. static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
  288. struct device_attribute *attr, const char *buf, size_t count)
  289. {
  290. struct qeth_card *card = dev_get_drvdata(dev);
  291. bool enable;
  292. int rc = 0;
  293. if (!card)
  294. return -EINVAL;
  295. mutex_lock(&card->conf_mutex);
  296. if ((card->state != CARD_STATE_DOWN) &&
  297. (card->state != CARD_STATE_RECOVER)) {
  298. rc = -EPERM;
  299. goto out;
  300. }
  301. if (sysfs_streq(buf, "toggle")) {
  302. enable = !card->ipato.enabled;
  303. } else if (kstrtobool(buf, &enable)) {
  304. rc = -EINVAL;
  305. goto out;
  306. }
  307. if (card->ipato.enabled != enable) {
  308. card->ipato.enabled = enable;
  309. spin_lock_bh(&card->ip_lock);
  310. qeth_l3_update_ipato(card);
  311. spin_unlock_bh(&card->ip_lock);
  312. }
  313. out:
  314. mutex_unlock(&card->conf_mutex);
  315. return rc ? rc : count;
  316. }
  317. static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
  318. qeth_l3_dev_ipato_enable_show,
  319. qeth_l3_dev_ipato_enable_store);
  320. static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
  321. struct device_attribute *attr, char *buf)
  322. {
  323. struct qeth_card *card = dev_get_drvdata(dev);
  324. if (!card)
  325. return -EINVAL;
  326. return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
  327. }
  328. static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
  329. struct device_attribute *attr,
  330. const char *buf, size_t count)
  331. {
  332. struct qeth_card *card = dev_get_drvdata(dev);
  333. bool invert;
  334. int rc = 0;
  335. if (!card)
  336. return -EINVAL;
  337. mutex_lock(&card->conf_mutex);
  338. if (sysfs_streq(buf, "toggle")) {
  339. invert = !card->ipato.invert4;
  340. } else if (kstrtobool(buf, &invert)) {
  341. rc = -EINVAL;
  342. goto out;
  343. }
  344. if (card->ipato.invert4 != invert) {
  345. card->ipato.invert4 = invert;
  346. spin_lock_bh(&card->ip_lock);
  347. qeth_l3_update_ipato(card);
  348. spin_unlock_bh(&card->ip_lock);
  349. }
  350. out:
  351. mutex_unlock(&card->conf_mutex);
  352. return rc ? rc : count;
  353. }
  354. static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
  355. qeth_l3_dev_ipato_invert4_show,
  356. qeth_l3_dev_ipato_invert4_store);
  357. static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
  358. enum qeth_prot_versions proto)
  359. {
  360. struct qeth_ipato_entry *ipatoe;
  361. char addr_str[40];
  362. int entry_len; /* length of 1 entry string, differs between v4 and v6 */
  363. int i = 0;
  364. entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
  365. /* add strlen for "/<mask>\n" */
  366. entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
  367. spin_lock_bh(&card->ip_lock);
  368. list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
  369. if (ipatoe->proto != proto)
  370. continue;
  371. /* String must not be longer than PAGE_SIZE. So we check if
  372. * string length gets near PAGE_SIZE. Then we can savely display
  373. * the next IPv6 address (worst case, compared to IPv4) */
  374. if ((PAGE_SIZE - i) <= entry_len)
  375. break;
  376. qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
  377. i += snprintf(buf + i, PAGE_SIZE - i,
  378. "%s/%i\n", addr_str, ipatoe->mask_bits);
  379. }
  380. spin_unlock_bh(&card->ip_lock);
  381. i += snprintf(buf + i, PAGE_SIZE - i, "\n");
  382. return i;
  383. }
  384. static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
  385. struct device_attribute *attr, char *buf)
  386. {
  387. struct qeth_card *card = dev_get_drvdata(dev);
  388. if (!card)
  389. return -EINVAL;
  390. return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
  391. }
  392. static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
  393. u8 *addr, int *mask_bits)
  394. {
  395. const char *start, *end;
  396. char *tmp;
  397. char buffer[40] = {0, };
  398. start = buf;
  399. /* get address string */
  400. end = strchr(start, '/');
  401. if (!end || (end - start >= 40)) {
  402. return -EINVAL;
  403. }
  404. strncpy(buffer, start, end - start);
  405. if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
  406. return -EINVAL;
  407. }
  408. start = end + 1;
  409. *mask_bits = simple_strtoul(start, &tmp, 10);
  410. if (!strlen(start) ||
  411. (tmp == start) ||
  412. (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
  413. return -EINVAL;
  414. }
  415. return 0;
  416. }
  417. static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
  418. struct qeth_card *card, enum qeth_prot_versions proto)
  419. {
  420. struct qeth_ipato_entry *ipatoe;
  421. u8 addr[16];
  422. int mask_bits;
  423. int rc = 0;
  424. mutex_lock(&card->conf_mutex);
  425. rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
  426. if (rc)
  427. goto out;
  428. ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
  429. if (!ipatoe) {
  430. rc = -ENOMEM;
  431. goto out;
  432. }
  433. ipatoe->proto = proto;
  434. memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
  435. ipatoe->mask_bits = mask_bits;
  436. rc = qeth_l3_add_ipato_entry(card, ipatoe);
  437. if (rc)
  438. kfree(ipatoe);
  439. out:
  440. mutex_unlock(&card->conf_mutex);
  441. return rc ? rc : count;
  442. }
  443. static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
  444. struct device_attribute *attr, const char *buf, size_t count)
  445. {
  446. struct qeth_card *card = dev_get_drvdata(dev);
  447. if (!card)
  448. return -EINVAL;
  449. return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
  450. }
  451. static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
  452. qeth_l3_dev_ipato_add4_show,
  453. qeth_l3_dev_ipato_add4_store);
  454. static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
  455. struct qeth_card *card, enum qeth_prot_versions proto)
  456. {
  457. u8 addr[16];
  458. int mask_bits;
  459. int rc = 0;
  460. mutex_lock(&card->conf_mutex);
  461. rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
  462. if (!rc)
  463. rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
  464. mutex_unlock(&card->conf_mutex);
  465. return rc ? rc : count;
  466. }
  467. static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
  468. struct device_attribute *attr, const char *buf, size_t count)
  469. {
  470. struct qeth_card *card = dev_get_drvdata(dev);
  471. if (!card)
  472. return -EINVAL;
  473. return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
  474. }
  475. static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
  476. qeth_l3_dev_ipato_del4_store);
  477. static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
  478. struct device_attribute *attr, char *buf)
  479. {
  480. struct qeth_card *card = dev_get_drvdata(dev);
  481. if (!card)
  482. return -EINVAL;
  483. return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
  484. }
  485. static ssize_t qeth_l3_dev_ipato_invert6_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. bool invert;
  490. int rc = 0;
  491. if (!card)
  492. return -EINVAL;
  493. mutex_lock(&card->conf_mutex);
  494. if (sysfs_streq(buf, "toggle")) {
  495. invert = !card->ipato.invert6;
  496. } else if (kstrtobool(buf, &invert)) {
  497. rc = -EINVAL;
  498. goto out;
  499. }
  500. if (card->ipato.invert6 != invert) {
  501. card->ipato.invert6 = invert;
  502. spin_lock_bh(&card->ip_lock);
  503. qeth_l3_update_ipato(card);
  504. spin_unlock_bh(&card->ip_lock);
  505. }
  506. out:
  507. mutex_unlock(&card->conf_mutex);
  508. return rc ? rc : count;
  509. }
  510. static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
  511. qeth_l3_dev_ipato_invert6_show,
  512. qeth_l3_dev_ipato_invert6_store);
  513. static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
  514. struct device_attribute *attr, char *buf)
  515. {
  516. struct qeth_card *card = dev_get_drvdata(dev);
  517. if (!card)
  518. return -EINVAL;
  519. return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
  520. }
  521. static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
  522. struct device_attribute *attr, const char *buf, size_t count)
  523. {
  524. struct qeth_card *card = dev_get_drvdata(dev);
  525. if (!card)
  526. return -EINVAL;
  527. return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
  528. }
  529. static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
  530. qeth_l3_dev_ipato_add6_show,
  531. qeth_l3_dev_ipato_add6_store);
  532. static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
  533. struct device_attribute *attr, const char *buf, size_t count)
  534. {
  535. struct qeth_card *card = dev_get_drvdata(dev);
  536. if (!card)
  537. return -EINVAL;
  538. return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
  539. }
  540. static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
  541. qeth_l3_dev_ipato_del6_store);
  542. static struct attribute *qeth_ipato_device_attrs[] = {
  543. &dev_attr_ipato_enable.attr,
  544. &dev_attr_ipato_invert4.attr,
  545. &dev_attr_ipato_add4.attr,
  546. &dev_attr_ipato_del4.attr,
  547. &dev_attr_ipato_invert6.attr,
  548. &dev_attr_ipato_add6.attr,
  549. &dev_attr_ipato_del6.attr,
  550. NULL,
  551. };
  552. static const struct attribute_group qeth_device_ipato_group = {
  553. .name = "ipa_takeover",
  554. .attrs = qeth_ipato_device_attrs,
  555. };
  556. static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf,
  557. enum qeth_prot_versions proto,
  558. enum qeth_ip_types type)
  559. {
  560. struct qeth_card *card = dev_get_drvdata(dev);
  561. struct qeth_ipaddr *ipaddr;
  562. char addr_str[40];
  563. int str_len = 0;
  564. int entry_len; /* length of 1 entry string, differs between v4 and v6 */
  565. int i;
  566. if (!card)
  567. return -EINVAL;
  568. entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
  569. entry_len += 2; /* \n + terminator */
  570. spin_lock_bh(&card->ip_lock);
  571. hash_for_each(card->ip_htable, i, ipaddr, hnode) {
  572. if (ipaddr->proto != proto || ipaddr->type != type)
  573. continue;
  574. /* String must not be longer than PAGE_SIZE. So we check if
  575. * string length gets near PAGE_SIZE. Then we can savely display
  576. * the next IPv6 address (worst case, compared to IPv4) */
  577. if ((PAGE_SIZE - str_len) <= entry_len)
  578. break;
  579. qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
  580. addr_str);
  581. str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n",
  582. addr_str);
  583. }
  584. spin_unlock_bh(&card->ip_lock);
  585. str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
  586. return str_len;
  587. }
  588. static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
  589. struct device_attribute *attr,
  590. char *buf)
  591. {
  592. return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
  593. QETH_IP_TYPE_VIPA);
  594. }
  595. static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
  596. u8 *addr)
  597. {
  598. if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
  599. return -EINVAL;
  600. }
  601. return 0;
  602. }
  603. static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
  604. struct qeth_card *card, enum qeth_prot_versions proto)
  605. {
  606. u8 addr[16] = {0, };
  607. int rc;
  608. mutex_lock(&card->conf_mutex);
  609. rc = qeth_l3_parse_vipae(buf, proto, addr);
  610. if (!rc)
  611. rc = qeth_l3_modify_rxip_vipa(card, true, addr,
  612. QETH_IP_TYPE_VIPA, proto);
  613. mutex_unlock(&card->conf_mutex);
  614. return rc ? rc : count;
  615. }
  616. static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
  617. struct device_attribute *attr, const char *buf, size_t count)
  618. {
  619. struct qeth_card *card = dev_get_drvdata(dev);
  620. if (!card)
  621. return -EINVAL;
  622. return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
  623. }
  624. static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
  625. qeth_l3_dev_vipa_add4_show,
  626. qeth_l3_dev_vipa_add4_store);
  627. static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
  628. struct qeth_card *card, enum qeth_prot_versions proto)
  629. {
  630. u8 addr[16];
  631. int rc;
  632. mutex_lock(&card->conf_mutex);
  633. rc = qeth_l3_parse_vipae(buf, proto, addr);
  634. if (!rc)
  635. rc = qeth_l3_modify_rxip_vipa(card, false, addr,
  636. QETH_IP_TYPE_VIPA, proto);
  637. mutex_unlock(&card->conf_mutex);
  638. return rc ? rc : count;
  639. }
  640. static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
  641. struct device_attribute *attr, const char *buf, size_t count)
  642. {
  643. struct qeth_card *card = dev_get_drvdata(dev);
  644. if (!card)
  645. return -EINVAL;
  646. return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
  647. }
  648. static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
  649. qeth_l3_dev_vipa_del4_store);
  650. static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
  651. struct device_attribute *attr,
  652. char *buf)
  653. {
  654. return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
  655. QETH_IP_TYPE_VIPA);
  656. }
  657. static ssize_t qeth_l3_dev_vipa_add6_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. if (!card)
  662. return -EINVAL;
  663. return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
  664. }
  665. static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
  666. qeth_l3_dev_vipa_add6_show,
  667. qeth_l3_dev_vipa_add6_store);
  668. static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
  669. struct device_attribute *attr, const char *buf, size_t count)
  670. {
  671. struct qeth_card *card = dev_get_drvdata(dev);
  672. if (!card)
  673. return -EINVAL;
  674. return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
  675. }
  676. static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
  677. qeth_l3_dev_vipa_del6_store);
  678. static struct attribute *qeth_vipa_device_attrs[] = {
  679. &dev_attr_vipa_add4.attr,
  680. &dev_attr_vipa_del4.attr,
  681. &dev_attr_vipa_add6.attr,
  682. &dev_attr_vipa_del6.attr,
  683. NULL,
  684. };
  685. static const struct attribute_group qeth_device_vipa_group = {
  686. .name = "vipa",
  687. .attrs = qeth_vipa_device_attrs,
  688. };
  689. static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
  690. struct device_attribute *attr,
  691. char *buf)
  692. {
  693. return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
  694. QETH_IP_TYPE_RXIP);
  695. }
  696. static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
  697. u8 *addr)
  698. {
  699. __be32 ipv4_addr;
  700. struct in6_addr ipv6_addr;
  701. if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
  702. return -EINVAL;
  703. }
  704. if (proto == QETH_PROT_IPV4) {
  705. memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
  706. if (ipv4_is_multicast(ipv4_addr)) {
  707. QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
  708. return -EINVAL;
  709. }
  710. } else if (proto == QETH_PROT_IPV6) {
  711. memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
  712. if (ipv6_addr_is_multicast(&ipv6_addr)) {
  713. QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
  714. return -EINVAL;
  715. }
  716. }
  717. return 0;
  718. }
  719. static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
  720. struct qeth_card *card, enum qeth_prot_versions proto)
  721. {
  722. u8 addr[16] = {0, };
  723. int rc;
  724. mutex_lock(&card->conf_mutex);
  725. rc = qeth_l3_parse_rxipe(buf, proto, addr);
  726. if (!rc)
  727. rc = qeth_l3_modify_rxip_vipa(card, true, addr,
  728. QETH_IP_TYPE_RXIP, proto);
  729. mutex_unlock(&card->conf_mutex);
  730. return rc ? rc : count;
  731. }
  732. static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
  733. struct device_attribute *attr, const char *buf, size_t count)
  734. {
  735. struct qeth_card *card = dev_get_drvdata(dev);
  736. if (!card)
  737. return -EINVAL;
  738. return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
  739. }
  740. static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
  741. qeth_l3_dev_rxip_add4_show,
  742. qeth_l3_dev_rxip_add4_store);
  743. static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
  744. struct qeth_card *card, enum qeth_prot_versions proto)
  745. {
  746. u8 addr[16];
  747. int rc;
  748. mutex_lock(&card->conf_mutex);
  749. rc = qeth_l3_parse_rxipe(buf, proto, addr);
  750. if (!rc)
  751. rc = qeth_l3_modify_rxip_vipa(card, false, addr,
  752. QETH_IP_TYPE_RXIP, proto);
  753. mutex_unlock(&card->conf_mutex);
  754. return rc ? rc : count;
  755. }
  756. static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
  757. struct device_attribute *attr, const char *buf, size_t count)
  758. {
  759. struct qeth_card *card = dev_get_drvdata(dev);
  760. if (!card)
  761. return -EINVAL;
  762. return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
  763. }
  764. static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
  765. qeth_l3_dev_rxip_del4_store);
  766. static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
  767. struct device_attribute *attr,
  768. char *buf)
  769. {
  770. return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
  771. QETH_IP_TYPE_RXIP);
  772. }
  773. static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
  774. struct device_attribute *attr, const char *buf, size_t count)
  775. {
  776. struct qeth_card *card = dev_get_drvdata(dev);
  777. if (!card)
  778. return -EINVAL;
  779. return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
  780. }
  781. static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
  782. qeth_l3_dev_rxip_add6_show,
  783. qeth_l3_dev_rxip_add6_store);
  784. static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
  785. struct device_attribute *attr, const char *buf, size_t count)
  786. {
  787. struct qeth_card *card = dev_get_drvdata(dev);
  788. if (!card)
  789. return -EINVAL;
  790. return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
  791. }
  792. static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
  793. qeth_l3_dev_rxip_del6_store);
  794. static struct attribute *qeth_rxip_device_attrs[] = {
  795. &dev_attr_rxip_add4.attr,
  796. &dev_attr_rxip_del4.attr,
  797. &dev_attr_rxip_add6.attr,
  798. &dev_attr_rxip_del6.attr,
  799. NULL,
  800. };
  801. static const struct attribute_group qeth_device_rxip_group = {
  802. .name = "rxip",
  803. .attrs = qeth_rxip_device_attrs,
  804. };
  805. static const struct attribute_group *qeth_l3_only_attr_groups[] = {
  806. &qeth_l3_device_attr_group,
  807. &qeth_device_ipato_group,
  808. &qeth_device_vipa_group,
  809. &qeth_device_rxip_group,
  810. NULL,
  811. };
  812. int qeth_l3_create_device_attributes(struct device *dev)
  813. {
  814. return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
  815. }
  816. void qeth_l3_remove_device_attributes(struct device *dev)
  817. {
  818. sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
  819. }
  820. const struct attribute_group *qeth_l3_attr_groups[] = {
  821. &qeth_device_attr_group,
  822. &qeth_device_blkt_group,
  823. /* l3 specific, see qeth_l3_only_attr_groups: */
  824. &qeth_l3_device_attr_group,
  825. &qeth_device_ipato_group,
  826. &qeth_device_vipa_group,
  827. &qeth_device_rxip_group,
  828. NULL,
  829. };