net.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* Minimal network stack: ARP + ICMP echo (ping) over a netdev NIC.
  2. Freestanding, no malloc; fixed-size buffers and a single configured IP. */
  3. #include "io.h"
  4. #include "netdev.h"
  5. #include "net.h"
  6. #include "timer.h"
  7. static unsigned char my_ip[4] = { 192, 168, 178, 240 };
  8. static unsigned char my_mask[4];
  9. static unsigned char my_gw[4];
  10. static unsigned char my_dns[4];
  11. /* ---- small print helpers (framebuffer console) ---- */
  12. static void print_hex2(unsigned int v)
  13. {
  14. static const char hex[] = "0123456789ABCDEF";
  15. io_putchar(hex[(v >> 4) & 0xF]);
  16. io_putchar(hex[v & 0xF]);
  17. }
  18. static void print_hex4(unsigned int v)
  19. {
  20. print_hex2((v >> 8) & 0xFF);
  21. print_hex2(v & 0xFF);
  22. }
  23. static void print_dec(unsigned int v)
  24. {
  25. char b[12];
  26. int i = 0;
  27. if (v == 0) {
  28. io_putchar('0');
  29. return;
  30. }
  31. while (v) {
  32. b[i++] = (char)('0' + (v % 10));
  33. v /= 10;
  34. }
  35. while (i)
  36. io_putchar(b[--i]);
  37. }
  38. static void print_mac(const unsigned char *m)
  39. {
  40. unsigned int i;
  41. for (i = 0; i < 6; i++) {
  42. if (i)
  43. io_putchar(':');
  44. print_hex2(m[i]);
  45. }
  46. }
  47. static void print_ip(const unsigned char *p)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < 4; i++) {
  51. if (i)
  52. io_putchar('.');
  53. print_dec(p[i]);
  54. }
  55. }
  56. static int ip_eq(const unsigned char *a, const unsigned char *b)
  57. {
  58. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  59. }
  60. /* Internet checksum (16-bit one's complement, network byte order). */
  61. unsigned short net_checksum(const void *data, unsigned int len)
  62. {
  63. const unsigned char *p = (const unsigned char *)data;
  64. unsigned int sum = 0;
  65. while (len > 1) {
  66. sum += ((unsigned int)p[0] << 8) | p[1];
  67. p += 2;
  68. len -= 2;
  69. }
  70. if (len)
  71. sum += (unsigned int)p[0] << 8;
  72. while (sum >> 16)
  73. sum = (sum & 0xFFFFu) + (sum >> 16);
  74. return (unsigned short)(~sum);
  75. }
  76. /* TCP checksum: the IPv4 pseudo-header plus the segment. */
  77. unsigned short net_checksum_tcp(const unsigned char *src_ip,
  78. const unsigned char *dst_ip,
  79. const unsigned char *tcp, unsigned int len)
  80. {
  81. const unsigned char *p = tcp;
  82. unsigned int sum = 0, i;
  83. for (i = 0; i < 4; i += 2)
  84. sum += ((unsigned int)src_ip[i] << 8) | src_ip[i + 1];
  85. for (i = 0; i < 4; i += 2)
  86. sum += ((unsigned int)dst_ip[i] << 8) | dst_ip[i + 1];
  87. sum += IPPROTO_TCP; /* pseudo-header protocol (0x0006) */
  88. sum += len; /* TCP length */
  89. while (len > 1) {
  90. sum += ((unsigned int)p[0] << 8) | p[1];
  91. p += 2;
  92. len -= 2;
  93. }
  94. if (len)
  95. sum += (unsigned int)p[0] << 8;
  96. while (sum >> 16)
  97. sum = (sum & 0xFFFFu) + (sum >> 16);
  98. return (unsigned short)(~sum);
  99. }
  100. /* ---- IP address config ---- */
  101. void net_set_ip(const unsigned char ip[4])
  102. {
  103. unsigned int i;
  104. /* Manual override: subnet knowledge is dropped, so the stack keeps
  105. * treating everything as directly reachable. */
  106. for (i = 0; i < 4; i++) {
  107. my_ip[i] = ip[i];
  108. my_mask[i] = 0;
  109. my_gw[i] = 0;
  110. }
  111. }
  112. void net_configure(const unsigned char ip[4], const unsigned char mask[4],
  113. const unsigned char gw[4])
  114. {
  115. unsigned int i;
  116. for (i = 0; i < 4; i++) {
  117. my_ip[i] = ip[i];
  118. my_mask[i] = mask ? mask[i] : 0;
  119. my_gw[i] = gw ? gw[i] : 0;
  120. }
  121. }
  122. void net_get_ip(unsigned char ip[4])
  123. {
  124. unsigned int i;
  125. for (i = 0; i < 4; i++)
  126. ip[i] = my_ip[i];
  127. }
  128. void net_get_mask(unsigned char mask[4])
  129. {
  130. unsigned int i;
  131. for (i = 0; i < 4; i++)
  132. mask[i] = my_mask[i];
  133. }
  134. void net_get_gateway(unsigned char gw[4])
  135. {
  136. unsigned int i;
  137. for (i = 0; i < 4; i++)
  138. gw[i] = my_gw[i];
  139. }
  140. int net_is_configured(void)
  141. {
  142. return my_ip[0] || my_ip[1] || my_ip[2] || my_ip[3];
  143. }
  144. void net_get_dns(unsigned char dns[4])
  145. {
  146. unsigned int i;
  147. for (i = 0; i < 4; i++)
  148. dns[i] = my_dns[i];
  149. }
  150. void net_set_dns(const unsigned char dns[4])
  151. {
  152. unsigned int i;
  153. for (i = 0; i < 4; i++)
  154. my_dns[i] = dns ? dns[i] : 0;
  155. }
  156. void net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  157. {
  158. unsigned int i, off = 0;
  159. for (i = 0; i < 4; i++)
  160. hop[i] = dst[i];
  161. /* Without a mask/gateway (manual netip) keep treating the target as
  162. * directly reachable, like ping does. */
  163. if (!(my_mask[0] || my_mask[1] || my_mask[2] || my_mask[3]) ||
  164. !(my_gw[0] || my_gw[1] || my_gw[2] || my_gw[3]))
  165. return;
  166. for (i = 0; i < 4; i++) {
  167. if ((dst[i] & my_mask[i]) != (my_ip[i] & my_mask[i])) {
  168. off = 1;
  169. break;
  170. }
  171. }
  172. if (off)
  173. for (i = 0; i < 4; i++)
  174. hop[i] = my_gw[i];
  175. }
  176. int net_parse_ip(const char *s, unsigned char ip[4])
  177. {
  178. unsigned int i, val = 0, nparts = 0;
  179. int have_digit = 0;
  180. for (i = 0; ; i++) {
  181. char c = s[i];
  182. if (c >= '0' && c <= '9') {
  183. val = val * 10 + (unsigned int)(c - '0');
  184. if (val > 255)
  185. return -1;
  186. have_digit = 1;
  187. } else if (c == '.' || c == '\0') {
  188. if (!have_digit || nparts >= 4)
  189. return -1;
  190. ip[nparts++] = (unsigned char)val;
  191. val = 0;
  192. have_digit = 0;
  193. if (c == '\0')
  194. break;
  195. } else {
  196. return -1;
  197. }
  198. }
  199. return (nparts == 4) ? 0 : -1;
  200. }
  201. void net_print_ip(void)
  202. {
  203. print_ip(my_ip);
  204. }
  205. /* ---- UDP send/parse (used by netlog, netkexec and the DHCP client) ---- */
  206. static unsigned char udp_frame[14 + 20 + 8 + NET_UDP_MAX];
  207. int net_udp_send_from(const unsigned char *src_ip,
  208. const unsigned char *dst_mac, const unsigned char *dst_ip,
  209. unsigned int src_port, unsigned int dst_port,
  210. const void *payload, unsigned int len)
  211. {
  212. const struct netdev *nd = netdev_active();
  213. unsigned char my_mac[6];
  214. unsigned char *ip = udp_frame + 14;
  215. unsigned char *udp = ip + 20;
  216. unsigned short cs;
  217. unsigned int i, total = 20 + 8 + len;
  218. const unsigned char *p = (const unsigned char *)payload;
  219. if (!nd || len > NET_UDP_MAX)
  220. return -1;
  221. nd->get_mac(my_mac);
  222. for (i = 0; i < 6; i++)
  223. udp_frame[i] = dst_mac[i];
  224. for (i = 0; i < 6; i++)
  225. udp_frame[6 + i] = my_mac[i];
  226. udp_frame[12] = 0x08;
  227. udp_frame[13] = 0x00;
  228. for (i = 0; i < 20; i++)
  229. ip[i] = 0;
  230. ip[0] = 0x45;
  231. ip[2] = (unsigned char)(total >> 8);
  232. ip[3] = (unsigned char)(total & 0xFF);
  233. ip[8] = 64;
  234. ip[9] = IPPROTO_UDP;
  235. for (i = 0; i < 4; i++)
  236. ip[12 + i] = src_ip[i];
  237. for (i = 0; i < 4; i++)
  238. ip[16 + i] = dst_ip[i];
  239. cs = net_checksum(ip, 20);
  240. ip[10] = (unsigned char)(cs >> 8);
  241. ip[11] = (unsigned char)(cs & 0xFF);
  242. udp[0] = (unsigned char)(src_port >> 8);
  243. udp[1] = (unsigned char)(src_port & 0xFF);
  244. udp[2] = (unsigned char)(dst_port >> 8);
  245. udp[3] = (unsigned char)(dst_port & 0xFF);
  246. udp[4] = (unsigned char)((8 + len) >> 8);
  247. udp[5] = (unsigned char)((8 + len) & 0xFF);
  248. udp[6] = 0; /* checksum disabled (allowed for IPv4) */
  249. udp[7] = 0;
  250. for (i = 0; i < len; i++)
  251. udp[8 + i] = p[i];
  252. return nd->tx(udp_frame, 14 + total);
  253. }
  254. int net_udp_send(const unsigned char *dst_mac, const unsigned char *dst_ip,
  255. unsigned int src_port, unsigned int dst_port,
  256. const void *payload, unsigned int len)
  257. {
  258. unsigned char my_ip[4];
  259. net_get_ip(my_ip);
  260. return net_udp_send_from(my_ip, dst_mac, dst_ip, src_port, dst_port,
  261. payload, len);
  262. }
  263. int net_udp_parse(const unsigned char *f, unsigned int len,
  264. unsigned char src_ip[4], unsigned char dst_ip[4],
  265. unsigned int *src_port, unsigned int *dst_port,
  266. const unsigned char **payload, unsigned int *plen)
  267. {
  268. const unsigned char *ip = f + 14;
  269. const unsigned char *udp;
  270. unsigned char my_ip[4];
  271. unsigned int i, ihl, ip_len;
  272. int dst_ok = 0, dst_bcast = 1;
  273. if (len < 14 + 20 + 8)
  274. return 0;
  275. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  276. return 0;
  277. if ((ip[0] >> 4) != 4)
  278. return 0;
  279. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  280. if (ihl < 20 || ip[9] != IPPROTO_UDP)
  281. return 0;
  282. ip_len = ((unsigned int)ip[2] << 8) | ip[3];
  283. if (ip_len < ihl + 8 || 14 + ip_len > len)
  284. return 0;
  285. /* Accept datagrams for us or for the broadcast address (a DHCP client
  286. * has no address yet). */
  287. net_get_ip(my_ip);
  288. dst_ok = 1;
  289. for (i = 0; i < 4; i++) {
  290. if (ip[16 + i] != my_ip[i])
  291. dst_ok = 0;
  292. if (ip[16 + i] != 0xFF)
  293. dst_bcast = 0;
  294. }
  295. if (!dst_ok && !dst_bcast)
  296. return 0;
  297. udp = ip + ihl;
  298. for (i = 0; i < 4; i++) {
  299. src_ip[i] = ip[12 + i];
  300. dst_ip[i] = ip[16 + i];
  301. }
  302. *src_port = ((unsigned int)udp[0] << 8) | udp[1];
  303. *dst_port = ((unsigned int)udp[2] << 8) | udp[3];
  304. *payload = udp + 8;
  305. *plen = ip_len - ihl - 8;
  306. return 1;
  307. }
  308. /* ---- ARP resolution (shared by the DNS resolver and the TCP client) ---- */
  309. /* ARP request "who has target? tell my_ip" -> 42-byte frame. */
  310. static unsigned int arp_build_request(const unsigned char *my_mac,
  311. const unsigned char *my_ip,
  312. const unsigned char *target_ip,
  313. unsigned char *out)
  314. {
  315. unsigned char *a = out + 14;
  316. unsigned int i;
  317. for (i = 0; i < 6; i++)
  318. out[i] = 0xFF; /* broadcast */
  319. for (i = 0; i < 6; i++)
  320. out[6 + i] = my_mac[i];
  321. out[12] = 0x08;
  322. out[13] = 0x06;
  323. a[0] = 0; a[1] = 1; /* htype Ethernet */
  324. a[2] = 0x08; a[3] = 0; /* ptype IPv4 */
  325. a[4] = 6; a[5] = 4;
  326. a[6] = 0; a[7] = 1; /* opcode request */
  327. for (i = 0; i < 6; i++) /* sha */
  328. a[8 + i] = my_mac[i];
  329. for (i = 0; i < 4; i++) /* spa */
  330. a[14 + i] = my_ip[i];
  331. for (i = 0; i < 6; i++) /* tha */
  332. a[18 + i] = 0;
  333. for (i = 0; i < 4; i++) /* tpa */
  334. a[24 + i] = target_ip[i];
  335. return 42;
  336. }
  337. /* 1 + peer MAC if frame is an ARP reply from target_ip. */
  338. static int arp_parse_reply(const unsigned char *f, unsigned int len,
  339. const unsigned char *target_ip, unsigned char *mac)
  340. {
  341. const unsigned char *a = f + 14;
  342. unsigned int i;
  343. if (len < 14 + 28)
  344. return 0;
  345. if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
  346. return 0;
  347. if ((((unsigned int)a[6] << 8) | a[7]) != 2) /* opcode reply */
  348. return 0;
  349. if (a[4] != 6 || a[5] != 4)
  350. return 0;
  351. if (!ip_eq(a + 14, target_ip)) /* sender IP == target */
  352. return 0;
  353. for (i = 0; i < 6; i++)
  354. mac[i] = a[8 + i];
  355. return 1;
  356. }
  357. int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6])
  358. {
  359. const struct netdev *nd = netdev_active();
  360. unsigned char my_mac[6], my_ip[4];
  361. unsigned char req[64];
  362. unsigned char *rxbuf = netdev_rx_scratch();
  363. unsigned int tries;
  364. if (!nd || !nd->get_mac)
  365. return -2;
  366. nd->get_mac(my_mac);
  367. net_get_ip(my_ip);
  368. for (tries = 0; tries < 3; tries++) {
  369. unsigned int deadline = get_ticks_ms() + 1000;
  370. unsigned int reqlen = arp_build_request(my_mac, my_ip,
  371. dst_ip, req);
  372. if (nd->tx(req, reqlen) != 0)
  373. return -2;
  374. while ((int)(get_ticks_ms() - deadline) < 0) {
  375. int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
  376. if (len < 0)
  377. continue;
  378. if (arp_parse_reply(rxbuf, (unsigned int)len,
  379. dst_ip, mac))
  380. return 0;
  381. }
  382. }
  383. return -1;
  384. }
  385. /* ---- ethertype naming ---- */
  386. const char *net_ethertype_name(unsigned short et)
  387. {
  388. switch (et) {
  389. case ET_IPV4: return "IPv4";
  390. case ET_ARP: return "ARP";
  391. case ET_IPV6: return "IPv6";
  392. default: return 0;
  393. }
  394. }
  395. /* ---- frame decoding ---- */
  396. void net_decode_frame(const unsigned char *f, unsigned int len)
  397. {
  398. unsigned short et;
  399. const char *name;
  400. if (len < 14) {
  401. io_putstr("(short frame len=");
  402. print_dec(len);
  403. io_putstr(")");
  404. return;
  405. }
  406. et = ((unsigned short)f[12] << 8) | f[13];
  407. print_mac(f + 6);
  408. io_putstr(" -> ");
  409. print_mac(f);
  410. io_putstr(" ");
  411. name = net_ethertype_name(et);
  412. if (name)
  413. io_putstr(name);
  414. else {
  415. io_putstr("0x");
  416. print_hex4(et);
  417. }
  418. io_putstr(" len=");
  419. print_dec(len);
  420. if (et == ET_ARP && len >= 14 + 28) {
  421. const unsigned char *a = f + 14;
  422. unsigned short op = ((unsigned short)a[6] << 8) | a[7];
  423. if (op == 1) {
  424. io_putstr(" who-has ");
  425. print_ip(a + 24);
  426. io_putstr(" tell ");
  427. print_ip(a + 14);
  428. } else if (op == 2) {
  429. io_putstr(" ");
  430. print_ip(a + 14);
  431. io_putstr(" is-at ");
  432. print_mac(a + 8);
  433. }
  434. } else if (et == ET_IPV4 && len >= 14 + 20) {
  435. const unsigned char *ip = f + 14;
  436. unsigned int ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  437. unsigned char proto = ip[9];
  438. io_putstr(" ");
  439. print_ip(ip + 12);
  440. io_putstr(" -> ");
  441. print_ip(ip + 16);
  442. io_putstr(" proto=");
  443. print_dec(proto);
  444. if (proto == IPPROTO_ICMP && ihl >= 20 && len >= 14 + ihl + 8) {
  445. const unsigned char *ic = ip + ihl;
  446. io_putstr(" ICMP type=");
  447. print_dec(ic[0]);
  448. io_putstr(" code=");
  449. print_dec(ic[1]);
  450. }
  451. }
  452. }
  453. /* ---- reply generation ---- */
  454. int net_build_reply(const unsigned char *frame, unsigned int len,
  455. unsigned char *out, unsigned int maxout)
  456. {
  457. unsigned char mac[6];
  458. unsigned short et;
  459. unsigned int i;
  460. if (len < 14)
  461. return 0;
  462. et = ((unsigned short)frame[12] << 8) | frame[13];
  463. netdev_get_mac(mac);
  464. /* ARP request for our IP -> ARP reply. */ if (et == ET_ARP && len >= 14 + 28) {
  465. const unsigned char *a = frame + 14;
  466. unsigned short op = ((unsigned short)a[6] << 8) | a[7];
  467. if (op != 1)
  468. return 0;
  469. if ((((unsigned short)a[0] << 8) | a[1]) != 1) /* htype Ethernet */
  470. return 0;
  471. if ((((unsigned short)a[2] << 8) | a[3]) != ET_IPV4)
  472. return 0;
  473. if (a[4] != 6 || a[5] != 4)
  474. return 0;
  475. if (!ip_eq(a + 24, my_ip)) /* target != us */
  476. return 0;
  477. for (i = 0; i < 6; i++)
  478. out[i] = frame[6 + i]; /* dst = requester */
  479. for (i = 0; i < 6; i++)
  480. out[6 + i] = mac[i]; /* src = us */
  481. out[12] = 0x08; out[13] = 0x06; /* ethertype ARP */
  482. {
  483. unsigned char *r = out + 14;
  484. r[0] = 0x00; r[1] = 0x01; /* htype Ethernet */
  485. r[2] = 0x08; r[3] = 0x00; /* ptype IPv4 */
  486. r[4] = 6; r[5] = 4;
  487. r[6] = 0x00; r[7] = 0x02; /* opcode reply */
  488. for (i = 0; i < 6; i++)
  489. r[8 + i] = mac[i]; /* sha */
  490. for (i = 0; i < 4; i++)
  491. r[14 + i] = my_ip[i]; /* spa */
  492. for (i = 0; i < 6; i++)
  493. r[18 + i] = a[8 + i]; /* tha = requester MAC */
  494. for (i = 0; i < 4; i++)
  495. r[24 + i] = a[14 + i]; /* tpa = requester IP */
  496. }
  497. return 42;
  498. }
  499. /* ICMP echo request for our IP -> ICMP echo reply. */
  500. if (et == ET_IPV4 && len >= 14 + 20) {
  501. const unsigned char *ip = frame + 14;
  502. unsigned int ihl, icmp_len, reply_len;
  503. unsigned short ip_len;
  504. const unsigned char *ic;
  505. unsigned char *rip, *ric;
  506. if ((ip[0] >> 4) != 4)
  507. return 0;
  508. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  509. if (ihl < 20)
  510. return 0;
  511. if (ip[9] != IPPROTO_ICMP)
  512. return 0;
  513. if (!ip_eq(ip + 16, my_ip))
  514. return 0;
  515. ip_len = ((unsigned short)ip[2] << 8) | ip[3];
  516. if (ip_len < ihl || ip_len > len - 14)
  517. return 0;
  518. if (len < 14 + ihl + 8)
  519. return 0;
  520. ic = ip + ihl;
  521. if (ic[0] != ICMP_ECHO_REQUEST)
  522. return 0;
  523. icmp_len = ip_len - ihl;
  524. reply_len = 14 + ip_len;
  525. if (reply_len > maxout)
  526. return 0;
  527. /* Ethernet header. */
  528. for (i = 0; i < 6; i++)
  529. out[i] = frame[6 + i];
  530. for (i = 0; i < 6; i++)
  531. out[6 + i] = mac[i];
  532. out[12] = 0x08; out[13] = 0x00; /* ethertype IPv4 */
  533. /* IPv4 header (IHL 5, no options). */
  534. rip = out + 14;
  535. rip[0] = 0x45;
  536. rip[1] = 0x00;
  537. rip[2] = ip[2]; rip[3] = ip[3]; /* total length (same) */
  538. rip[4] = ip[4]; rip[5] = ip[5]; /* id */
  539. rip[6] = 0x00; rip[7] = 0x00; /* flags/frag */
  540. rip[8] = 64; /* TTL */
  541. rip[9] = IPPROTO_ICMP;
  542. rip[10] = 0; rip[11] = 0; /* checksum (computed below) */
  543. for (i = 0; i < 4; i++)
  544. rip[12 + i] = my_ip[i]; /* src = us */
  545. for (i = 0; i < 4; i++)
  546. rip[16 + i] = ip[12 + i]; /* dst = requester */
  547. /* ICMP: copy the whole request, then fix type/code/checksum. */
  548. ric = rip + ihl;
  549. for (i = 0; i < icmp_len; i++)
  550. ric[i] = ic[i];
  551. ric[0] = ICMP_ECHO_REPLY;
  552. ric[1] = 0;
  553. ric[2] = 0; ric[3] = 0;
  554. {
  555. unsigned short cs = net_checksum(rip, ihl);
  556. rip[10] = (unsigned char)(cs >> 8);
  557. rip[11] = (unsigned char)(cs & 0xFF);
  558. }
  559. {
  560. unsigned short cs = net_checksum(ric, icmp_len);
  561. ric[2] = (unsigned char)(cs >> 8);
  562. ric[3] = (unsigned char)(cs & 0xFF);
  563. }
  564. return (int)reply_len;
  565. }
  566. return 0;
  567. }