nmrp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * nmrpflash is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <time.h>
  26. #include "nmrpd.h"
  27. #define NMRP_HDR_LEN 6
  28. #define NMRP_OPT_HDR_LEN 4
  29. #define NMRP_MIN_PKT_LEN (sizeof(struct eth_hdr) + NMRP_HDR_LEN)
  30. #define ETH_P_NMRP 0x0912
  31. #ifndef PACKED
  32. #define PACKED __attribute__((__packed__))
  33. #endif
  34. #ifdef NMRPFLASH_WINDOWS
  35. #define setenv(name, value, overwrite) SetEnvironmentVariable(name, value)
  36. #endif
  37. enum nmrp_code {
  38. NMRP_C_NONE = 0,
  39. NMRP_C_ADVERTISE = 1,
  40. NMRP_C_CONF_REQ = 2,
  41. NMRP_C_CONF_ACK = 3,
  42. NMRP_C_CLOSE_REQ = 4,
  43. NMRP_C_CLOSE_ACK = 5,
  44. NMRP_C_KEEP_ALIVE_REQ = 6,
  45. NMRP_C_KEEP_ALIVE_ACK = 7,
  46. NMRP_C_TFTP_UL_REQ = 16
  47. };
  48. enum nmrp_opt_type {
  49. NMRP_O_MAGIC_NO = 0x0001,
  50. NMRP_O_DEV_IP = 0x0002,
  51. NMRP_O_DEV_REGION = 0x0004,
  52. NMRP_O_FW_UP = 0x0101,
  53. NMRP_O_ST_UP = 0x0102,
  54. NMRP_O_FILE_NAME = 0x0181
  55. };
  56. struct nmrp_opt {
  57. uint16_t type;
  58. uint16_t len;
  59. char val[1];
  60. } PACKED;
  61. struct nmrp_msg {
  62. uint16_t reserved;
  63. uint8_t code;
  64. uint8_t id;
  65. uint16_t len;
  66. char opts[44];
  67. } PACKED;
  68. struct nmrp_pkt {
  69. struct eth_hdr eh;
  70. struct nmrp_msg msg;
  71. } PACKED;
  72. static const char *msg_code_str(uint16_t code)
  73. {
  74. #define MSG_CODE(x) case NMRP_C_ ## x: return #x
  75. static char buf[16];
  76. switch (code) {
  77. MSG_CODE(ADVERTISE);
  78. MSG_CODE(CONF_REQ);
  79. MSG_CODE(CONF_ACK);
  80. MSG_CODE(CLOSE_REQ);
  81. MSG_CODE(CLOSE_ACK);
  82. MSG_CODE(KEEP_ALIVE_REQ);
  83. MSG_CODE(KEEP_ALIVE_ACK);
  84. MSG_CODE(TFTP_UL_REQ);
  85. default:
  86. snprintf(buf, sizeof(buf), "%04x", ntohs(code));
  87. return buf;
  88. }
  89. #undef MSG_CODE
  90. }
  91. static uint16_t to_region_code(const char *region)
  92. {
  93. #define REGION_CODE(r, c) if (!strcasecmp(region, r)) return htons(c)
  94. REGION_CODE("NA", 0x0001);
  95. REGION_CODE("WW", 0x0002);
  96. REGION_CODE("GR", 0x0003);
  97. REGION_CODE("PR", 0x0004);
  98. REGION_CODE("RU", 0x0005);
  99. REGION_CODE("BZ", 0x0006);
  100. REGION_CODE("IN", 0x0007);
  101. REGION_CODE("KO", 0x0008);
  102. REGION_CODE("JP", 0x0009);
  103. #undef REGION_CODE
  104. return 0;
  105. }
  106. static void msg_dump(struct nmrp_msg *msg)
  107. {
  108. int rem;
  109. fprintf(stderr, "res=0x%04x, code=0x%02x, id=0x%02x, len=%u",
  110. ntohs(msg->reserved), msg->code, msg->id, ntohs(msg->len));
  111. rem = ntohs(msg->len) - NMRP_HDR_LEN;
  112. fprintf(stderr, "%s\n", rem ? "" : " (no opts)");
  113. }
  114. static void *msg_opt(struct nmrp_msg *msg, uint16_t type, uint16_t* len)
  115. {
  116. struct nmrp_opt* opt = (struct nmrp_opt*)msg->opts;
  117. size_t rem = ntohs(msg->len) - NMRP_HDR_LEN;
  118. uint16_t olen;
  119. do {
  120. olen = ntohs(opt->len);
  121. if (olen < NMRP_OPT_HDR_LEN || olen > rem) {
  122. break;
  123. }
  124. if (ntohs(opt->type) == type) {
  125. if (len) {
  126. *len = olen;
  127. }
  128. return opt->val;
  129. }
  130. rem -= olen;
  131. } while (rem);
  132. return NULL;
  133. }
  134. static char *msg_filename(struct nmrp_msg *msg)
  135. {
  136. static char buf[256];
  137. uint16_t len;
  138. char *p = msg_opt(msg, NMRP_O_FILE_NAME, &len);
  139. if (p) {
  140. len = MIN(sizeof(buf) - 1, len);
  141. memcpy(buf, p, len);
  142. buf[len] = '\0';
  143. return buf;
  144. }
  145. return NULL;
  146. }
  147. static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
  148. {
  149. memset(msg, 0, sizeof(*msg));
  150. msg->len = htons(NMRP_HDR_LEN);
  151. msg->code = code;
  152. }
  153. static char *msg_mkopt(struct nmrp_msg *msg, char *p, uint16_t type, const void *val, size_t len)
  154. {
  155. struct nmrp_opt* opt = (struct nmrp_opt*)p;
  156. len &= 0xffff;
  157. msg->len = ntohs(msg->len);
  158. if ((msg->len + len > sizeof(*msg))) {
  159. fprintf(stderr, "Error: invalid option - this is a bug\n");
  160. exit(1);
  161. }
  162. opt->type = htons(type);
  163. opt->len = NMRP_OPT_HDR_LEN + len;
  164. if (val) {
  165. memcpy(opt->val, val, len);
  166. }
  167. msg->len += opt->len;
  168. p += opt->len;
  169. msg->len = htons(msg->len);
  170. opt->len = htons(opt->len);
  171. return p;
  172. }
  173. static void msg_mkadvertise(struct nmrp_msg *msg, const char *magic)
  174. {
  175. msg_init(msg, NMRP_C_ADVERTISE);
  176. msg_mkopt(msg, msg->opts, NMRP_O_MAGIC_NO, magic, strlen(magic));
  177. }
  178. static void msg_mkconfack(struct nmrp_msg *msg, uint32_t ipaddr, uint32_t ipmask, uint16_t region)
  179. {
  180. char *p;
  181. struct {
  182. uint32_t addr;
  183. uint32_t mask;
  184. } PACKED ip = {
  185. .addr = ipaddr,
  186. .mask = ipmask
  187. };
  188. msg_init(msg, NMRP_C_CONF_ACK);
  189. p = msg_mkopt(msg, msg->opts, NMRP_O_DEV_IP, &ip, 8);
  190. p = msg_mkopt(msg, p, NMRP_O_FW_UP, NULL, 0);
  191. #ifdef NMRPFLASH_SET_REGION
  192. if (region) {
  193. p = msg_mkopt(msg, p, NMRP_O_DEV_REGION, &region, 2);
  194. }
  195. #endif
  196. }
  197. #ifdef NMRPFLASH_FUZZ
  198. #define NMRP_INITIAL_TIMEOUT 0
  199. #define ethsock_create(a, b) ((struct ethsock*)1)
  200. #define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
  201. #define ethsock_recv(sock, buf, len) read(STDIN_FILENO, buf, len)
  202. #define ethsock_send(a, b, c) (0)
  203. #define ethsock_set_timeout(a, b) (0)
  204. #define ethsock_ip_add(a, b, c, d) (0)
  205. #define ethsock_ip_del(a, b) (0)
  206. #define ethsock_close(a) (0)
  207. #define tftp_put(a) (0)
  208. static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
  209. {
  210. static uint8_t hwaddr[6] = { 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa };
  211. return hwaddr;
  212. }
  213. #else
  214. #define NMRP_INITIAL_TIMEOUT 60
  215. #endif
  216. static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
  217. {
  218. return ethsock_send(sock, pkt, sizeof(*pkt));
  219. }
  220. static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
  221. {
  222. ssize_t bytes, len;
  223. memset(pkt, 0, sizeof(*pkt));
  224. bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
  225. if (bytes < 0) {
  226. return 1;
  227. } else if (!bytes) {
  228. return 2;
  229. } else if (bytes < NMRP_MIN_PKT_LEN) {
  230. fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
  231. return 1;
  232. }
  233. len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
  234. if (bytes < len) {
  235. fprintf(stderr, "Short packet (expected %d, got %d).\n",
  236. (int)len, (int)bytes);
  237. return 1;
  238. } else if (bytes > sizeof(pkt->msg) + sizeof(pkt->eh)) {
  239. fprintf(stderr, "Packet size exceeds maximum (got %d).\n",
  240. (int)bytes);
  241. }
  242. return 0;
  243. }
  244. static int mac_parse(const char *str, uint8_t *hwaddr)
  245. {
  246. int i;
  247. unsigned data[6];
  248. sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
  249. data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
  250. if (i == strlen(str)) {
  251. for (i = 0; i != 6; ++i) {
  252. if (data[i] > 255) {
  253. break;
  254. }
  255. hwaddr[i] = data[i] & 0xff;
  256. }
  257. if (i == 6) {
  258. return 1;
  259. }
  260. }
  261. return 0;
  262. }
  263. struct is_valid_ip_arg
  264. {
  265. struct in_addr *ipaddr;
  266. struct in_addr *ipmask;
  267. int result;
  268. };
  269. static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
  270. {
  271. #define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
  272. struct is_valid_ip_arg *arg = args->arg;
  273. if (SUBNET(args) == SUBNET(arg)) {
  274. arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
  275. return 0;
  276. }
  277. return 1;
  278. #undef SUBNET
  279. }
  280. static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
  281. struct in_addr *ipmask)
  282. {
  283. int status;
  284. struct is_valid_ip_arg arg = {
  285. .ipaddr = ipaddr,
  286. .ipmask = ipmask,
  287. .result = 0
  288. };
  289. status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
  290. return status < 0 ? status : arg.result;
  291. }
  292. static void sigh(int sig)
  293. {
  294. g_interrupted = 1;
  295. }
  296. static const char *spinner = "\\|/-";
  297. int nmrp_do(struct nmrpd_args *args)
  298. {
  299. struct nmrp_pkt tx, rx;
  300. uint8_t *src, dest[6];
  301. uint16_t region;
  302. char *filename;
  303. time_t beg;
  304. int i, status, ulreqs, expect, upload_ok, autoip;
  305. struct ethsock *sock;
  306. struct ethsock_ip_undo *ip_undo = NULL;
  307. struct ethsock_arp_undo *arp_undo = NULL;
  308. uint32_t intf_addr;
  309. void (*sigh_orig)(int);
  310. struct {
  311. struct in_addr addr;
  312. struct in_addr mask;
  313. } PACKED ipconf;
  314. if (args->op != NMRP_UPLOAD_FW) {
  315. fprintf(stderr, "Operation not implemented.\n");
  316. return 1;
  317. }
  318. if (!mac_parse(args->mac, dest)) {
  319. fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
  320. return 1;
  321. }
  322. ipconf.mask.s_addr = inet_addr(args->ipmask);
  323. if (ipconf.mask.s_addr == INADDR_NONE
  324. || netmask(bitcount(ipconf.mask.s_addr)) != ipconf.mask.s_addr) {
  325. fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
  326. return 1;
  327. }
  328. if (!args->ipaddr) {
  329. autoip = true;
  330. /* The MAC of the device that was used to test this utility starts
  331. * with a4:2b:8c, hence 164 (0xa4) and 183 (0x2b + 0x8c)
  332. */
  333. args->ipaddr = "10.164.183.252";
  334. if (!args->ipaddr_intf) {
  335. args->ipaddr_intf = "10.164.183.253";
  336. }
  337. } else if (args->ipaddr_intf) {
  338. autoip = true;
  339. } else {
  340. autoip = false;
  341. }
  342. if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
  343. fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
  344. return 1;
  345. }
  346. if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
  347. fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
  348. return 1;
  349. }
  350. if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
  351. fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
  352. return 1;
  353. }
  354. if (args->file_remote) {
  355. if (!tftp_is_valid_filename(args->file_remote)) {
  356. fprintf(stderr, "Invalid remote filename '%s'.\n",
  357. args->file_remote);
  358. return 1;
  359. }
  360. }
  361. if (args->region) {
  362. region = to_region_code(args->region);
  363. if (!region) {
  364. fprintf(stderr, "Invalid region code '%s'.\n", args->region);
  365. return 1;
  366. }
  367. } else {
  368. region = 0;
  369. }
  370. status = 1;
  371. sock = ethsock_create(args->intf, ETH_P_NMRP);
  372. if (!sock) {
  373. return 1;
  374. }
  375. sigh_orig = signal(SIGINT, sigh);
  376. if (!autoip) {
  377. status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
  378. if (status <= 0) {
  379. if (!status) {
  380. fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
  381. args->ipaddr, args->ipmask, args->intf);
  382. }
  383. goto out;
  384. }
  385. } else {
  386. if (verbosity) {
  387. printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
  388. }
  389. if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &ip_undo) != 0) {
  390. goto out;
  391. }
  392. }
  393. if (ethsock_set_timeout(sock, args->rx_timeout)) {
  394. goto out;
  395. }
  396. src = ethsock_get_hwaddr(sock);
  397. if (!src) {
  398. goto out;
  399. }
  400. memcpy(tx.eh.ether_shost, src, 6);
  401. memcpy(tx.eh.ether_dhost, dest, 6);
  402. tx.eh.ether_type = htons(ETH_P_NMRP);
  403. msg_mkadvertise(&tx.msg, "NTGR");
  404. i = 0;
  405. upload_ok = 0;
  406. beg = time_monotonic();
  407. while (!g_interrupted) {
  408. printf("\rAdvertising NMRP server on %s ... %c",
  409. args->intf, spinner[i]);
  410. fflush(stdout);
  411. i = (i + 1) & 3;
  412. if (pkt_send(sock, &tx) < 0) {
  413. xperror("sendto");
  414. goto out;
  415. }
  416. status = pkt_recv(sock, &rx);
  417. if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) {
  418. break;
  419. } else if (status == 1) {
  420. goto out;
  421. } else {
  422. /* because we don't want nmrpflash's exit status to be zero */
  423. status = 1;
  424. if ((time_monotonic() - beg) >= NMRP_INITIAL_TIMEOUT) {
  425. printf("\nNo response after 60 seconds. Bailing out.\n");
  426. goto out;
  427. }
  428. }
  429. }
  430. printf("\n");
  431. expect = NMRP_C_CONF_REQ;
  432. ulreqs = 0;
  433. while (!g_interrupted) {
  434. if (expect != NMRP_C_NONE && rx.msg.code != expect) {
  435. fprintf(stderr, "Received %s while waiting for %s!\n",
  436. msg_code_str(rx.msg.code), msg_code_str(expect));
  437. }
  438. msg_init(&tx.msg, NMRP_C_NONE);
  439. status = 1;
  440. switch (rx.msg.code) {
  441. case NMRP_C_ADVERTISE:
  442. printf("Received NMRP advertisement from %s.\n",
  443. mac_to_str(rx.eh.ether_shost));
  444. status = 1;
  445. goto out;
  446. case NMRP_C_CONF_REQ:
  447. msg_mkconfack(&tx.msg, ipconf.addr.s_addr, ipconf.mask.s_addr, region);
  448. expect = NMRP_C_TFTP_UL_REQ;
  449. printf("Received configuration request from %s.\n",
  450. mac_to_str(rx.eh.ether_shost));
  451. memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
  452. printf("Sending configuration: %s, netmask %s.\n",
  453. args->ipaddr, args->ipmask);
  454. if (ethsock_arp_add(sock, rx.eh.ether_shost, ipconf.addr.s_addr, &arp_undo) != 0) {
  455. goto out;
  456. }
  457. break;
  458. case NMRP_C_TFTP_UL_REQ:
  459. if (!upload_ok) {
  460. if (++ulreqs > 5) {
  461. printf("Bailing out after %d upload requests.\n",
  462. ulreqs);
  463. tx.msg.code = NMRP_C_CLOSE_REQ;
  464. break;
  465. }
  466. } else {
  467. if (verbosity) {
  468. printf("Ignoring extra upload request.\n");
  469. }
  470. ethsock_set_timeout(sock, args->ul_timeout);
  471. tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
  472. break;
  473. }
  474. filename = msg_filename(&rx.msg);
  475. if (filename) {
  476. if (!args->file_remote) {
  477. args->file_remote = filename;
  478. }
  479. printf("Received upload request: filename '%s'.\n", filename);
  480. } else if (!args->file_remote) {
  481. args->file_remote = args->file_local;
  482. printf("Received upload request with empty filename.\n");
  483. }
  484. status = 0;
  485. if (args->tftpcmd) {
  486. printf("Executing '%s' ... \n", args->tftpcmd);
  487. setenv("IP", inet_ntoa(ipconf.addr), 1);
  488. setenv("PORT", lltostr(args->port, 10), 1);
  489. setenv("MAC", mac_to_str(rx.eh.ether_shost), 1);
  490. setenv("NETMASK", inet_ntoa(ipconf.mask), 1);
  491. //setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
  492. status = system(args->tftpcmd);
  493. }
  494. if (!status && args->file_local) {
  495. if (!autoip) {
  496. status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask);
  497. if (status < 0) {
  498. goto out;
  499. } else if (!status) {
  500. printf("IP address of %s has changed. Please assign a "
  501. "static ip to the interface.\n", args->intf);
  502. tx.msg.code = NMRP_C_CLOSE_REQ;
  503. break;
  504. }
  505. }
  506. if (verbosity) {
  507. printf("Using remote filename '%s'.\n",
  508. args->file_remote);
  509. }
  510. if (!strcmp(args->file_local, "-")) {
  511. printf("Uploading from stdin ... ");
  512. } else {
  513. printf("Uploading %s ... ", leafname(args->file_local));
  514. }
  515. fflush(stdout);
  516. if (!(status = tftp_put(args))) {
  517. printf("OK\n");
  518. }
  519. }
  520. if (!status) {
  521. printf("Waiting for remote to respond.\n");
  522. upload_ok = 1;
  523. ethsock_set_timeout(sock, args->ul_timeout);
  524. tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
  525. expect = NMRP_C_NONE;
  526. } else if (status == -2) {
  527. expect = NMRP_C_TFTP_UL_REQ;
  528. } else {
  529. goto out;
  530. }
  531. break;
  532. case NMRP_C_KEEP_ALIVE_REQ:
  533. tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
  534. ethsock_set_timeout(sock, args->ul_timeout);
  535. printf("Received keep-alive request.\n");
  536. break;
  537. case NMRP_C_CLOSE_REQ:
  538. tx.msg.code = NMRP_C_CLOSE_ACK;
  539. break;
  540. case NMRP_C_CLOSE_ACK:
  541. status = 0;
  542. goto out;
  543. default:
  544. fprintf(stderr, "Unknown message code 0x%02x!\n",
  545. rx.msg.code);
  546. msg_dump(&rx.msg);
  547. }
  548. if (tx.msg.code != NMRP_C_NONE) {
  549. if (pkt_send(sock, &tx) < 0) {
  550. xperror("sendto");
  551. goto out;
  552. }
  553. if (tx.msg.code == NMRP_C_CLOSE_REQ) {
  554. goto out;
  555. }
  556. }
  557. if (rx.msg.code == NMRP_C_CLOSE_REQ) {
  558. printf("Remote finished. Closing connection.\n");
  559. break;
  560. }
  561. status = pkt_recv(sock, &rx);
  562. if (status) {
  563. if (status == 2) {
  564. fprintf(stderr, "Timeout while waiting for %s.\n",
  565. msg_code_str(expect));
  566. }
  567. goto out;
  568. }
  569. ethsock_set_timeout(sock, args->rx_timeout);
  570. }
  571. if (!g_interrupted) {
  572. status = 0;
  573. if (ulreqs) {
  574. printf("Reboot your device now.\n");
  575. } else {
  576. printf("No upload request received.\n");
  577. }
  578. }
  579. out:
  580. signal(SIGINT, sigh_orig);
  581. ethsock_arp_del(sock, &arp_undo);
  582. ethsock_ip_del(sock, &ip_undo);
  583. ethsock_close(sock);
  584. return status;
  585. }