nmrp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. opt = (struct nmrp_opt*)(((char *)opt) + olen);
  131. rem -= olen;
  132. } while (rem);
  133. return NULL;
  134. }
  135. static char *msg_filename(struct nmrp_msg *msg)
  136. {
  137. static char buf[256];
  138. uint16_t len;
  139. char *p = msg_opt(msg, NMRP_O_FILE_NAME, &len);
  140. if (p) {
  141. len = MIN(sizeof(buf) - 1, len);
  142. memcpy(buf, p, len);
  143. buf[len] = '\0';
  144. return buf;
  145. }
  146. return NULL;
  147. }
  148. static inline void msg_init(struct nmrp_msg *msg, uint16_t code)
  149. {
  150. memset(msg, 0, sizeof(*msg));
  151. msg->len = htons(NMRP_HDR_LEN);
  152. msg->code = code;
  153. }
  154. static char *msg_mkopt(struct nmrp_msg *msg, char *p, uint16_t type, const void *val, size_t len)
  155. {
  156. struct nmrp_opt* opt = (struct nmrp_opt*)p;
  157. len &= 0xffff;
  158. msg->len = ntohs(msg->len);
  159. if ((msg->len + len > sizeof(*msg))) {
  160. fprintf(stderr, "Error: invalid option - this is a bug\n");
  161. exit(1);
  162. }
  163. opt->type = htons(type);
  164. opt->len = NMRP_OPT_HDR_LEN + len;
  165. if (val) {
  166. memcpy(opt->val, val, len);
  167. }
  168. msg->len += opt->len;
  169. p += opt->len;
  170. msg->len = htons(msg->len);
  171. opt->len = htons(opt->len);
  172. return p;
  173. }
  174. static void msg_mkadvertise(struct nmrp_msg *msg, const char *magic)
  175. {
  176. msg_init(msg, NMRP_C_ADVERTISE);
  177. msg_mkopt(msg, msg->opts, NMRP_O_MAGIC_NO, magic, strlen(magic));
  178. }
  179. static void msg_mkconfack(struct nmrp_msg *msg, uint32_t ipaddr, uint32_t ipmask, uint16_t region)
  180. {
  181. char *p;
  182. uint32_t ip[2] = { ipaddr, ipmask };
  183. msg_init(msg, NMRP_C_CONF_ACK);
  184. p = msg_mkopt(msg, msg->opts, NMRP_O_DEV_IP, &ip, 8);
  185. p = msg_mkopt(msg, p, NMRP_O_FW_UP, NULL, 0);
  186. #ifdef NMRPFLASH_SET_REGION
  187. if (region) {
  188. p = msg_mkopt(msg, p, NMRP_O_DEV_REGION, &region, 2);
  189. }
  190. #endif
  191. }
  192. #ifdef NMRPFLASH_FUZZ
  193. #define NMRP_ADVERTISE_TIMEOUT 0
  194. #define ethsock_create(a, b) ((struct ethsock*)1)
  195. #define ethsock_get_hwaddr(a) ethsock_get_hwaddr_fake(a)
  196. #define ethsock_recv(sock, buf, len) read(STDIN_FILENO, buf, len)
  197. #define ethsock_send(a, b, c) (0)
  198. #define ethsock_set_timeout(a, b) (0)
  199. #define ethsock_arp_add(a, b, c, d) (0)
  200. #define ethsock_arp_del(a, b) (0)
  201. #define ethsock_ip_add(a, b, c, d) (0)
  202. #define ethsock_ip_del(a, b) (0)
  203. #define ethsock_close(a) (0)
  204. #define ethsock_for_each_ip(a, b, c) (1)
  205. #define tftp_put(a) (0)
  206. static uint8_t *ethsock_get_hwaddr_fake(struct ethsock* sock)
  207. {
  208. static uint8_t hwaddr[6] = { 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa };
  209. return hwaddr;
  210. }
  211. #else
  212. #define NMRP_ADVERTISE_TIMEOUT 60
  213. #endif
  214. static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
  215. {
  216. return ethsock_send(sock, pkt, sizeof(pkt->eh) + ntohs(pkt->msg.len));
  217. }
  218. static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
  219. {
  220. ssize_t bytes, mlen;
  221. memset(pkt, 0, sizeof(*pkt));
  222. bytes = ethsock_recv(sock, pkt, sizeof(*pkt));
  223. if (bytes < 0) {
  224. return 1;
  225. } else if (!bytes) {
  226. return 2;
  227. }
  228. mlen = ntohs(pkt->msg.len);
  229. if (bytes < (mlen + sizeof(pkt->eh))
  230. || bytes < NMRP_MIN_PKT_LEN
  231. || mlen < NMRP_HDR_LEN) {
  232. fprintf(stderr, "Short packet (%d raw, %d message)\n",
  233. (int)bytes, (int)mlen);
  234. return 1;
  235. } else if (mlen > sizeof(pkt->msg)) {
  236. printf("Truncating %d byte message.\n", (int)mlen);
  237. pkt->msg.len = htons(sizeof(pkt->msg));
  238. }
  239. return 0;
  240. }
  241. static int mac_parse(const char *str, uint8_t *hwaddr)
  242. {
  243. int i;
  244. unsigned data[6];
  245. sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x%n",
  246. data, data + 1, data + 2, data + 3, data + 4, data + 5, &i);
  247. if (i == strlen(str)) {
  248. for (i = 0; i != 6; ++i) {
  249. if (data[i] > 255) {
  250. break;
  251. }
  252. hwaddr[i] = data[i] & 0xff;
  253. }
  254. if (i == 6) {
  255. return 1;
  256. }
  257. }
  258. return 0;
  259. }
  260. static bool mac_is_broadcast(uint8_t* hwaddr)
  261. {
  262. for (int i = 0; i != 6; ++i) {
  263. if (hwaddr[i] != 0xff) {
  264. return false;
  265. }
  266. }
  267. return true;
  268. }
  269. struct is_valid_ip_arg
  270. {
  271. struct in_addr *ipaddr;
  272. struct in_addr *ipmask;
  273. int result;
  274. };
  275. static int is_valid_ip_cb(struct ethsock_ip_callback_args *args)
  276. {
  277. #define SUBNET(x) ((x)->ipaddr->s_addr & (x)->ipmask->s_addr)
  278. struct is_valid_ip_arg *arg = args->arg;
  279. if (SUBNET(args) == SUBNET(arg)) {
  280. arg->result = args->ipaddr->s_addr != arg->ipaddr->s_addr;
  281. return 0;
  282. }
  283. return 1;
  284. #undef SUBNET
  285. }
  286. static int is_valid_ip(struct ethsock *sock, struct in_addr *ipaddr,
  287. struct in_addr *ipmask)
  288. {
  289. int status;
  290. struct is_valid_ip_arg arg = {
  291. .ipaddr = ipaddr,
  292. .ipmask = ipmask,
  293. .result = 0
  294. };
  295. status = ethsock_for_each_ip(sock, is_valid_ip_cb, &arg);
  296. return status < 0 ? status : arg.result;
  297. }
  298. static void sigh(int sig)
  299. {
  300. g_interrupted = 1;
  301. }
  302. bool nmrp_discard(struct ethsock *sock)
  303. {
  304. // between nmrpflash sending the TFTP WRQ packet, and the router
  305. // responding with ACK(0)/OACK, some devices send extraneous
  306. // CONF_REQ and/or TFTP_UL_REQ packets.
  307. //
  308. // the TFTP code thus calls this function in each loop iteration,
  309. // to discard any late NMRP packets.
  310. //
  311. // without this it might seem as if these packets arrived after
  312. // the TFTP upload completed successfuly, confusing the NMRP code.
  313. unsigned timeout = ethsock_get_timeout(sock);
  314. // don't set this to 0, as this would cause pkt_recv to block!
  315. ethsock_set_timeout(sock, 1);
  316. struct nmrp_pkt rx;
  317. int ret = pkt_recv(sock, &rx);
  318. if (ret == 0) {
  319. if (rx.msg.code != NMRP_C_CONF_REQ && rx.msg.code != NMRP_C_TFTP_UL_REQ) {
  320. printf("Discarding unexpected %s packet\n", msg_code_str(rx.msg.code));
  321. } else if (verbosity > 1) {
  322. printf("Discarding %s packet\n", msg_code_str(rx.msg.code));
  323. }
  324. }
  325. ethsock_set_timeout(sock, timeout);
  326. return ret == 0;
  327. }
  328. static const char *spinner = "\\|/-";
  329. int nmrp_do(struct nmrpd_args *args)
  330. {
  331. struct nmrp_pkt tx, rx;
  332. uint8_t *src, dest[6];
  333. uint16_t region;
  334. char *filename;
  335. time_t beg;
  336. int i, timeout, status, ulreqs, expect, upload_ok, autoip, ka_reqs;
  337. bool was_plugged_in;
  338. ssize_t bytes;
  339. struct ethsock *sock;
  340. struct ethsock_ip_undo *ip_undo = NULL;
  341. struct ethsock_arp_undo *arp_undo = NULL;
  342. uint32_t intf_addr = 0;
  343. void (*sigh_orig)(int);
  344. struct in_addr ipaddr;
  345. struct in_addr ipmask;
  346. if (args->op != NMRP_UPLOAD_FW) {
  347. fprintf(stderr, "Operation not implemented.\n");
  348. return 1;
  349. }
  350. if (!mac_parse(args->mac, dest)) {
  351. fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac);
  352. return 1;
  353. }
  354. ipmask.s_addr = inet_addr(args->ipmask);
  355. if (ipmask.s_addr == INADDR_NONE
  356. || netmask(bitcount(ipmask.s_addr)) != ipmask.s_addr) {
  357. fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
  358. return 1;
  359. }
  360. if (!args->ipaddr) {
  361. autoip = true;
  362. args->ipaddr = NMRP_DEFAULT_IP_REMOTE;
  363. if (!args->ipaddr_intf) {
  364. args->ipaddr_intf = NMRP_DEFAULT_IP_LOCAL;
  365. }
  366. } else if (args->ipaddr_intf) {
  367. autoip = true;
  368. } else {
  369. autoip = false;
  370. }
  371. if ((ipaddr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
  372. fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
  373. return 1;
  374. }
  375. if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
  376. fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf);
  377. return 1;
  378. }
  379. if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) {
  380. fprintf(stderr, "Error accessing file '%s'.\n", args->file_local);
  381. return 1;
  382. }
  383. if (args->file_remote) {
  384. if (!tftp_is_valid_filename(args->file_remote)) {
  385. fprintf(stderr, "Invalid remote filename '%s'.\n",
  386. args->file_remote);
  387. return 1;
  388. }
  389. }
  390. if (args->region) {
  391. region = to_region_code(args->region);
  392. if (!region) {
  393. fprintf(stderr, "Invalid region code '%s'.\n", args->region);
  394. return 1;
  395. }
  396. } else {
  397. region = 0;
  398. }
  399. status = 1;
  400. sock = ethsock_create(args->intf, ETH_P_NMRP);
  401. if (!sock) {
  402. return 1;
  403. }
  404. args->sock = sock;
  405. sigh_orig = signal(SIGINT, sigh);
  406. was_plugged_in = !ethsock_is_unplugged(sock);
  407. if (!was_plugged_in) {
  408. if (ethsock_is_wifi(sock)) {
  409. fprintf(stderr, "Error: Wi-Fi not connected.\n");
  410. goto out;
  411. }
  412. printf("Waiting for Ethernet connection (Ctrl-C to skip).\n");
  413. bool unplugged = true;
  414. time_t beg = time_monotonic();
  415. while (!g_interrupted && (time_monotonic() - beg) < 20) {
  416. if (!ethsock_is_unplugged(sock)) {
  417. unplugged = false;
  418. break;
  419. }
  420. }
  421. was_plugged_in = !unplugged;
  422. if (unplugged) {
  423. if (!g_interrupted) {
  424. fprintf(stderr, "Error: Ethernet cable is unplugged.\n");
  425. goto out;
  426. } else {
  427. printf("\rSkipped.\n");
  428. g_interrupted = false;
  429. }
  430. }
  431. }
  432. if (ethsock_is_wifi(sock)) {
  433. printf("Warning: using a Wi-Fi interface. Make sure you know what you're doing!\n");
  434. }
  435. if (!autoip) {
  436. status = is_valid_ip(sock, &ipaddr, &ipmask);
  437. if (status <= 0) {
  438. if (!status) {
  439. fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n",
  440. args->ipaddr, args->ipmask, args->intf);
  441. }
  442. goto out;
  443. }
  444. } else {
  445. if (verbosity) {
  446. printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf);
  447. }
  448. if (ethsock_ip_add(sock, intf_addr, ipmask.s_addr, &ip_undo) != 0) {
  449. goto out;
  450. }
  451. }
  452. if (ethsock_set_timeout(sock, 20)) {
  453. goto out;
  454. }
  455. src = ethsock_get_hwaddr(sock);
  456. if (!src) {
  457. goto out;
  458. }
  459. memcpy(tx.eh.ether_shost, src, 6);
  460. memcpy(tx.eh.ether_dhost, dest, 6);
  461. tx.eh.ether_type = htons(ETH_P_NMRP);
  462. msg_mkadvertise(&tx.msg, "NTGR");
  463. i = 0;
  464. upload_ok = 0;
  465. timeout = args->blind ? 10 : NMRP_ADVERTISE_TIMEOUT;
  466. beg = time_monotonic();
  467. printf("Advertising NMRP server on %s ... ", args->intf);
  468. while (!g_interrupted) {
  469. i = (i + 1) & 3;
  470. printf("%c ", spinner[i]);
  471. fflush(stdout);
  472. printf("\b\b");
  473. was_plugged_in |= !ethsock_is_unplugged(sock);
  474. if (pkt_send(sock, &tx) < 0) {
  475. goto out;
  476. }
  477. status = pkt_recv(sock, &rx);
  478. if (status == 0) {
  479. if (memcmp(rx.eh.ether_dhost, src, 6) == 0) {
  480. // don't continue in blind mode if we've received a response
  481. args->blind = false;
  482. break;
  483. } else if (verbosity) {
  484. printf("\nIgnoring bogus response: %s -> %s.\n",
  485. mac_to_str(rx.eh.ether_shost),
  486. mac_to_str(rx.eh.ether_dhost));
  487. }
  488. } else if (status == 1) {
  489. goto out;
  490. } else {
  491. /* because we don't want nmrpflash's exit status to be zero */
  492. status = 1;
  493. if ((time_monotonic() - beg) >= timeout) {
  494. printf("\nNo response after %d seconds. ", timeout);
  495. if (!args->blind || !was_plugged_in) {
  496. if (!was_plugged_in) {
  497. printf("Ethernet cable unplugged. ");
  498. }
  499. printf("Bailing out.\n");
  500. goto out;
  501. } else {
  502. // we're blind, so fake a response from the MAC specified by -m
  503. memcpy(rx.eh.ether_shost, dest, 6);
  504. msg_init(&rx.msg, NMRP_C_CONF_REQ);
  505. printf("Continuing blindly.");
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. printf("\n");
  512. memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
  513. // a manually specified MAC address (using `-m`) takes precedence over
  514. // the NMRP response packets' MAC.
  515. uint8_t* arp_mac = !mac_is_broadcast(dest) ? dest : rx.eh.ether_shost;
  516. if (ethsock_arp_add(sock, arp_mac, ipaddr.s_addr, &arp_undo) != 0) {
  517. goto out;
  518. }
  519. if (ethsock_set_timeout(sock, args->rx_timeout)) {
  520. goto out;
  521. }
  522. expect = NMRP_C_CONF_REQ;
  523. ulreqs = 0;
  524. ka_reqs = 0;
  525. while (!g_interrupted) {
  526. if (expect != NMRP_C_NONE && rx.msg.code != expect) {
  527. fprintf(stderr, "Received %s while waiting for %s!\n",
  528. msg_code_str(rx.msg.code), msg_code_str(expect));
  529. }
  530. msg_init(&tx.msg, NMRP_C_NONE);
  531. status = 1;
  532. switch (rx.msg.code) {
  533. case NMRP_C_ADVERTISE:
  534. printf("Received NMRP advertisement from %s.\n",
  535. mac_to_str(rx.eh.ether_shost));
  536. status = 1;
  537. goto out;
  538. case NMRP_C_CONF_REQ:
  539. msg_mkconfack(&tx.msg, ipaddr.s_addr, ipmask.s_addr, region);
  540. expect = NMRP_C_TFTP_UL_REQ;
  541. if (!args->blind) {
  542. printf("Received configuration request from %s.\n",
  543. mac_to_str(rx.eh.ether_shost));
  544. }
  545. printf("Sending configuration: %s/%d.\n",
  546. args->ipaddr, bitcount(ipmask.s_addr));
  547. break;
  548. case NMRP_C_TFTP_UL_REQ:
  549. if (!upload_ok) {
  550. if (++ulreqs > 5) {
  551. printf("Bailing out after %d upload requests.\n",
  552. ulreqs);
  553. tx.msg.code = NMRP_C_CLOSE_REQ;
  554. break;
  555. }
  556. } else {
  557. if (verbosity) {
  558. printf("Ignoring extra upload request.\n");
  559. }
  560. ethsock_set_timeout(sock, args->ul_timeout);
  561. break;
  562. }
  563. filename = msg_filename(&rx.msg);
  564. if (filename) {
  565. if (!args->file_remote) {
  566. args->file_remote = filename;
  567. }
  568. printf("Received upload request: filename '%s'.\n", filename);
  569. } else if (!args->file_remote) {
  570. args->file_remote = leafname(args->file_local);
  571. if (!args->blind) {
  572. printf("Received upload request.\n");
  573. }
  574. }
  575. if (args->tftpcmd) {
  576. printf("Executing '%s' ... \n", args->tftpcmd);
  577. setenv("IP", inet_ntoa(ipaddr), 1);
  578. setenv("PORT", lltostr(args->port, 10), 1);
  579. setenv("MAC", mac_to_str(arp_mac), 1);
  580. setenv("NETMASK", inet_ntoa(ipmask), 1);
  581. //setenv("FILENAME", args->file_remote ? args->file_remote : "", 1);
  582. setenv("INTERFACE", args->intf, 1);
  583. status = system(args->tftpcmd);
  584. if (status != 0) {
  585. fprintf(stderr, "Command failed: status %d.\n", status);
  586. goto out;
  587. }
  588. }
  589. if (args->file_local) {
  590. if (!autoip) {
  591. status = is_valid_ip(sock, &ipaddr, &ipmask);
  592. if (status < 0) {
  593. goto out;
  594. } else if (!status) {
  595. printf("IP address of %s has changed. Please assign a "
  596. "static ip to the interface.\n", args->intf);
  597. tx.msg.code = NMRP_C_CLOSE_REQ;
  598. break;
  599. }
  600. }
  601. if (verbosity) {
  602. printf("Using remote filename '%s'.\n",
  603. args->file_remote);
  604. }
  605. if (!strcmp(args->file_local, "-")) {
  606. printf("Uploading from stdin ... ");
  607. } else {
  608. printf("Uploading %s ... ", leafname(args->file_local));
  609. }
  610. fflush(stdout);
  611. bytes = tftp_put(args);
  612. if (bytes > 0) {
  613. printf("OK (%zd b)\n", bytes);
  614. upload_ok = 1;
  615. if (args->blind) {
  616. printf("Not waiting for further responses in blind mode.\n");
  617. goto out;
  618. }
  619. } else if (bytes == -2) {
  620. // return -2 means the TFTP code signalled that the remote
  621. // file has been rejected. this feature is only implemented
  622. // by some bootloaders.
  623. expect = NMRP_C_TFTP_UL_REQ;
  624. } else {
  625. goto out;
  626. }
  627. } else {
  628. // if no `-f <filename>` flag has been supplied, assume
  629. // that the command specified by `-c <command>` handles
  630. // the file upload.
  631. upload_ok = 1;
  632. }
  633. if (upload_ok) {
  634. printf("Waiting for remote to respond.\n");
  635. ethsock_set_timeout(sock, args->ul_timeout);
  636. tx.msg.code = NMRP_C_NONE;
  637. expect = NMRP_C_NONE;
  638. }
  639. break;
  640. case NMRP_C_KEEP_ALIVE_REQ:
  641. tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
  642. ethsock_set_timeout(sock, args->ul_timeout);
  643. printf("\rReceived keep-alive request (%d). ", ++ka_reqs);
  644. break;
  645. case NMRP_C_CLOSE_REQ:
  646. tx.msg.code = NMRP_C_CLOSE_ACK;
  647. break;
  648. case NMRP_C_CLOSE_ACK:
  649. status = 0;
  650. goto out;
  651. default:
  652. fprintf(stderr, "Unknown message code 0x%02x!\n",
  653. rx.msg.code);
  654. msg_dump(&rx.msg);
  655. }
  656. if (tx.msg.code != NMRP_C_NONE) {
  657. if (pkt_send(sock, &tx) != 0 || tx.msg.code == NMRP_C_CLOSE_REQ) {
  658. goto out;
  659. }
  660. }
  661. if (rx.msg.code == NMRP_C_CLOSE_REQ) {
  662. if (ka_reqs) {
  663. printf("\n");
  664. }
  665. printf("Remote finished. Closing connection.\n");
  666. break;
  667. }
  668. status = pkt_recv(sock, &rx);
  669. if (status) {
  670. if (status == 2) {
  671. if (!args->blind) {
  672. fprintf(stderr, "Timeout while waiting for %s.\n",
  673. msg_code_str(expect));
  674. goto out;
  675. }
  676. // fake response
  677. msg_init(&rx.msg, expect);
  678. memcpy(rx.eh.ether_shost, tx.eh.ether_dhost, 6);
  679. } else {
  680. goto out;
  681. }
  682. }
  683. ethsock_set_timeout(sock, args->rx_timeout);
  684. }
  685. if (!g_interrupted) {
  686. status = 0;
  687. if (ulreqs) {
  688. printf("Reboot your device now.\n");
  689. } else {
  690. printf("No upload request received.\n");
  691. }
  692. }
  693. out:
  694. signal(SIGINT, sigh_orig);
  695. ethsock_arp_del(sock, &arp_undo);
  696. ethsock_ip_del(sock, &ip_undo);
  697. ethsock_close(sock);
  698. return status;
  699. }