nmrp.c 16 KB

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