qeth_l3_sys.c 27 KB

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