bootp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "ip.h"
  16. int debugload;
  17. extern char *persist;
  18. uint8_t broadcast[Eaddrlen] = {
  19. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  20. };
  21. static uint16_t tftpport = 5000;
  22. static int Id = 1;
  23. static Netaddr myaddr;
  24. static Netaddr server;
  25. typedef struct {
  26. uint8_t header[4];
  27. uint8_t data[TFTP_SEGSIZE];
  28. } Tftp;
  29. static Tftp tftpb;
  30. static void
  31. hnputs(uint8_t *ptr, uint16_t val)
  32. {
  33. ptr[0] = val>>8;
  34. ptr[1] = val;
  35. }
  36. static void
  37. hnputl(uint8_t *ptr, uint32_t val)
  38. {
  39. ptr[0] = val>>24;
  40. ptr[1] = val>>16;
  41. ptr[2] = val>>8;
  42. ptr[3] = val;
  43. }
  44. static uint32_t
  45. nhgetl(uint8_t *ptr)
  46. {
  47. return ((ptr[0]<<24) | (ptr[1]<<16) | (ptr[2]<<8) | ptr[3]);
  48. }
  49. static uint16_t
  50. nhgets(uint8_t *ptr)
  51. {
  52. return ((ptr[0]<<8) | ptr[1]);
  53. }
  54. static int16_t endian = 1;
  55. static char* aendian = (char*)&endian;
  56. #define LITTLE *aendian
  57. static uint16_t
  58. ptcl_csum(void *a, int len)
  59. {
  60. uint8_t *addr;
  61. uint32_t t1, t2;
  62. uint32_t losum, hisum, mdsum, x;
  63. addr = a;
  64. losum = 0;
  65. hisum = 0;
  66. mdsum = 0;
  67. x = 0;
  68. if((uint32_t)addr & 1) {
  69. if(len) {
  70. hisum += addr[0];
  71. len--;
  72. addr++;
  73. }
  74. x = 1;
  75. }
  76. while(len >= 16) {
  77. t1 = *(uint16_t*)(addr+0);
  78. t2 = *(uint16_t*)(addr+2); mdsum += t1;
  79. t1 = *(uint16_t*)(addr+4); mdsum += t2;
  80. t2 = *(uint16_t*)(addr+6); mdsum += t1;
  81. t1 = *(uint16_t*)(addr+8); mdsum += t2;
  82. t2 = *(uint16_t*)(addr+10); mdsum += t1;
  83. t1 = *(uint16_t*)(addr+12); mdsum += t2;
  84. t2 = *(uint16_t*)(addr+14); mdsum += t1;
  85. mdsum += t2;
  86. len -= 16;
  87. addr += 16;
  88. }
  89. while(len >= 2) {
  90. mdsum += *(uint16_t*)addr;
  91. len -= 2;
  92. addr += 2;
  93. }
  94. if(x) {
  95. if(len)
  96. losum += addr[0];
  97. if(LITTLE)
  98. losum += mdsum;
  99. else
  100. hisum += mdsum;
  101. } else {
  102. if(len)
  103. hisum += addr[0];
  104. if(LITTLE)
  105. hisum += mdsum;
  106. else
  107. losum += mdsum;
  108. }
  109. losum += hisum >> 8;
  110. losum += (hisum & 0xff) << 8;
  111. while(hisum = losum>>16)
  112. losum = hisum + (losum & 0xffff);
  113. return ~losum;
  114. }
  115. static uint16_t
  116. ip_csum(uint8_t *addr)
  117. {
  118. int len;
  119. uint32_t sum = 0;
  120. len = (addr[0]&0xf)<<2;
  121. while(len > 0) {
  122. sum += addr[0]<<8 | addr[1] ;
  123. len -= 2;
  124. addr += 2;
  125. }
  126. sum = (sum & 0xffff) + (sum >> 16);
  127. sum = (sum & 0xffff) + (sum >> 16);
  128. return (sum^0xffff);
  129. }
  130. enum {
  131. /* this is only true of IPv4, but we're not doing v6 yet */
  132. Min_udp_payload = ETHERMINTU - ETHERHDRSIZE - UDP_HDRSIZE,
  133. };
  134. static void
  135. udpsend(int ctlrno, Netaddr *a, void *data, int dlen)
  136. {
  137. Udphdr *uh;
  138. Etherhdr *ip;
  139. Etherpkt pkt;
  140. int len, ptcllen;
  141. uh = (Udphdr*)&pkt;
  142. memset(uh, 0, sizeof(Etherpkt));
  143. memmove(uh->udpcksum+sizeof(uh->udpcksum), data, dlen);
  144. /*
  145. * UDP portion
  146. */
  147. ptcllen = dlen + (UDP_HDRSIZE-UDP_PHDRSIZE);
  148. uh->ttl = 0;
  149. uh->udpproto = IP_UDPPROTO;
  150. uh->frag[0] = 0;
  151. uh->frag[1] = 0;
  152. hnputs(uh->udpplen, ptcllen);
  153. hnputl(uh->udpsrc, myaddr.ip);
  154. hnputs(uh->udpsport, myaddr.port);
  155. hnputl(uh->udpdst, a->ip);
  156. hnputs(uh->udpdport, a->port);
  157. hnputs(uh->udplen, ptcllen);
  158. uh->udpcksum[0] = 0;
  159. uh->udpcksum[1] = 0;
  160. dlen = (dlen+1)&~1;
  161. hnputs(uh->udpcksum, ptcl_csum(&uh->ttl, dlen+UDP_HDRSIZE));
  162. /*
  163. * IP portion
  164. */
  165. ip = (Etherhdr*)&pkt;
  166. len = UDP_EHSIZE+UDP_HDRSIZE+dlen; /* non-descriptive names */
  167. ip->vihl = IP_VER|IP_HLEN;
  168. ip->tos = 0;
  169. ip->ttl = 255;
  170. hnputs(ip->length, len-ETHER_HDR);
  171. hnputs(ip->id, Id++);
  172. ip->frag[0] = 0;
  173. ip->frag[1] = 0;
  174. ip->cksum[0] = 0;
  175. ip->cksum[1] = 0;
  176. hnputs(ip->cksum, ip_csum(&ip->vihl));
  177. /*
  178. * Ethernet MAC portion
  179. */
  180. hnputs(ip->type, ET_IP);
  181. memmove(ip->d, a->ea, sizeof(ip->d));
  182. if(debug) {
  183. print("udpsend ");
  184. }
  185. /*
  186. * if packet is too short, make it longer rather than relying
  187. * on ethernet interface or lower layers to pad it.
  188. */
  189. if (len < ETHERMINTU)
  190. len = ETHERMINTU;
  191. ethertxpkt(ctlrno, &pkt, len, Timeout);
  192. }
  193. static void
  194. nak(int ctlrno, Netaddr *a, int code, char *msg, int report)
  195. {
  196. int n;
  197. char buf[128];
  198. buf[0] = 0;
  199. buf[1] = Tftp_ERROR;
  200. buf[2] = 0;
  201. buf[3] = code;
  202. strcpy(buf+4, msg);
  203. n = strlen(msg) + 4 + 1;
  204. udpsend(ctlrno, a, buf, n);
  205. if(report)
  206. print("\ntftp: error(%d): %s\n", code, msg);
  207. }
  208. static int
  209. udprecv(int ctlrno, Netaddr *a, void *data, int dlen)
  210. {
  211. int n, len, olen;
  212. uint8_t *dp;
  213. uint16_t csm;
  214. Udphdr *h;
  215. uint32_t addr, timo;
  216. Etherpkt pkt;
  217. static int rxactive;
  218. if(rxactive == 0)
  219. timo = 1000;
  220. else
  221. timo = Timeout;
  222. timo += TK2MS(machp()->ticks);
  223. while(timo > TK2MS(machp()->ticks)){
  224. n = etherrxpkt(ctlrno, &pkt, timo-TK2MS(machp()->ticks));
  225. if(n <= 0)
  226. continue;
  227. h = (Udphdr*)&pkt;
  228. if(debug)
  229. print("udprecv %E to %E...\n", h->s, h->d);
  230. if(nhgets(h->type) != ET_IP || (h->vihl & 0xf0) != IP_VER) {
  231. if(debug)
  232. print("not ipv4 (%#4.4ux) %#2.2ux...",
  233. nhgets(h->type), h->vihl);
  234. continue;
  235. }
  236. if(ip_csum(&h->vihl)) {
  237. print("ip chksum error\n");
  238. continue;
  239. }
  240. if((h->vihl & 0x0f) != IP_HLEN) {
  241. len = (h->vihl & 0xf)<<2;
  242. if(len < (IP_HLEN<<2)) {
  243. print("ip bad hlen %d %E to %E\n", len, h->s, h->d);
  244. continue;
  245. }
  246. /* strip off the options */
  247. olen = nhgets(h->length);
  248. dp = (uint8_t*)h + (len - (IP_HLEN<<2));
  249. memmove(dp, h, IP_HLEN<<2);
  250. h = (Udphdr*)dp;
  251. h->vihl = (IP_VER|IP_HLEN);
  252. hnputs(h->length, olen-len+(IP_HLEN<<2));
  253. if(debug)
  254. print("strip %d %E to %E\n", len, h->s, h->d);
  255. }
  256. if(h->udpproto != IP_UDPPROTO) {
  257. if(debug)
  258. print("not udp (%d)...", h->udpproto);
  259. continue;
  260. }
  261. if(debug)
  262. print("okay udp...");
  263. h->ttl = 0;
  264. len = nhgets(h->udplen);
  265. hnputs(h->udpplen, len);
  266. if(nhgets(h->udpcksum)) {
  267. csm = ptcl_csum(&h->ttl, len+UDP_PHDRSIZE);
  268. if(csm != 0) {
  269. print("udp chksum error csum #%4ux len %d\n",
  270. csm, n);
  271. break;
  272. }
  273. }
  274. if(a->port != 0 && nhgets(h->udpsport) != a->port) {
  275. if(debug)
  276. print("udpport %ux not %ux\n",
  277. nhgets(h->udpsport), a->port);
  278. continue;
  279. }
  280. addr = nhgetl(h->udpsrc);
  281. if(a->ip != Bcastip && a->ip != addr) {
  282. if(debug)
  283. print("bad ip %lux not %lux\n", addr, a->ip);
  284. continue;
  285. }
  286. len -= UDP_HDRSIZE-UDP_PHDRSIZE;
  287. if(len > dlen) {
  288. print("udp: packet too big: %d > %d; from addr %E\n",
  289. len, dlen, h->udpsrc);
  290. continue;
  291. }
  292. memmove(data, h->udpcksum+sizeof(h->udpcksum), len);
  293. a->ip = addr;
  294. a->port = nhgets(h->udpsport);
  295. memmove(a->ea, pkt.s, sizeof(a->ea));
  296. rxactive = 1;
  297. return len;
  298. }
  299. return 0;
  300. }
  301. static int tftpblockno;
  302. /*
  303. * format of a request packet, from the RFC:
  304. *
  305. 2 bytes string 1 byte string 1 byte
  306. ------------------------------------------------
  307. | Opcode | Filename | 0 | Mode | 0 |
  308. ------------------------------------------------
  309. */
  310. static int
  311. tftpopen(int ctlrno, Netaddr *a, char *name, Tftp *tftp)
  312. {
  313. int i, len, rlen, oport;
  314. char buf[TFTP_SEGSIZE+2];
  315. buf[0] = 0;
  316. buf[1] = Tftp_READ;
  317. len = 2 + sprint(buf+2, "%s", name) + 1;
  318. len += sprint(buf+len, "octet") + 1;
  319. oport = a->port;
  320. for(i = 0; i < 5; i++){
  321. a->port = oport;
  322. udpsend(ctlrno, a, buf, len);
  323. a->port = 0;
  324. if((rlen = udprecv(ctlrno, a, tftp, sizeof(Tftp))) < sizeof(tftp->header))
  325. continue;
  326. switch((tftp->header[0]<<8)|tftp->header[1]){
  327. case Tftp_ERROR:
  328. print("tftpopen: error (%d): %s\n",
  329. (tftp->header[2]<<8)|tftp->header[3],
  330. (char*)tftp->data);
  331. return -1;
  332. case Tftp_DATA:
  333. tftpblockno = 1;
  334. len = (tftp->header[2]<<8)|tftp->header[3];
  335. if(len != tftpblockno){
  336. print("tftpopen: block error: %d\n", len);
  337. nak(ctlrno, a, 1, "block error", 0);
  338. return -1;
  339. }
  340. rlen -= sizeof(tftp->header);
  341. if(rlen < TFTP_SEGSIZE){
  342. /* ACK now, in case we don't later */
  343. buf[0] = 0;
  344. buf[1] = Tftp_ACK;
  345. buf[2] = tftpblockno>>8;
  346. buf[3] = tftpblockno;
  347. udpsend(ctlrno, a, buf, sizeof(tftp->header));
  348. }
  349. return rlen;
  350. }
  351. }
  352. print("tftpopen: failed to connect to server\n");
  353. return -1;
  354. }
  355. static int
  356. tftpread(int ctlrno, Netaddr *a, Tftp *tftp, int dlen)
  357. {
  358. uint8_t buf[4];
  359. int try, blockno, len;
  360. dlen += sizeof(tftp->header);
  361. for(try = 0; try < 10; try++) {
  362. buf[0] = 0;
  363. buf[1] = Tftp_ACK;
  364. buf[2] = tftpblockno>>8;
  365. buf[3] = tftpblockno;
  366. udpsend(ctlrno, a, buf, sizeof(buf));
  367. len = udprecv(ctlrno, a, tftp, dlen);
  368. if(len <= sizeof(tftp->header)){
  369. if(debug)
  370. print("tftpread: too short %d <= %d\n",
  371. len, sizeof(tftp->header));
  372. continue;
  373. }
  374. blockno = (tftp->header[2]<<8)|tftp->header[3];
  375. if(blockno <= tftpblockno){
  376. if(debug)
  377. print("tftpread: blkno %d <= %d\n",
  378. blockno, tftpblockno);
  379. continue;
  380. }
  381. if(blockno == tftpblockno+1) {
  382. tftpblockno++;
  383. if(len < dlen) { /* last packet; send final ack */
  384. tftpblockno++;
  385. buf[0] = 0;
  386. buf[1] = Tftp_ACK;
  387. buf[2] = tftpblockno>>8;
  388. buf[3] = tftpblockno;
  389. udpsend(ctlrno, a, buf, sizeof(buf));
  390. }
  391. return len-sizeof(tftp->header);
  392. }
  393. print("tftpread: block error: %d, expected %d\n",
  394. blockno, tftpblockno+1);
  395. }
  396. return -1;
  397. }
  398. static int
  399. bootpopen(int ctlrno, char *file, Bootp *brep, int dotftpopen)
  400. {
  401. Bootp req, *rep;
  402. int i, n;
  403. uint8_t *ea, rpkt[1024];
  404. char name[128], *filename, *sysname;
  405. if (debugload)
  406. print("bootpopen: ether%d!%s...", ctlrno, file);
  407. if((ea = etheraddr(ctlrno)) == 0){
  408. print("invalid ctlrno %d\n", ctlrno);
  409. return -1;
  410. }
  411. filename = 0;
  412. sysname = 0;
  413. if(file && *file){
  414. strcpy(name, file);
  415. if(filename = strchr(name, '!')){
  416. sysname = name;
  417. *filename++ = 0;
  418. }
  419. else
  420. filename = name;
  421. }
  422. memset(&req, 0, sizeof(req));
  423. req.op = Bootrequest;
  424. req.htype = 1; /* ethernet */
  425. req.hlen = Eaddrlen; /* ethernet */
  426. memmove(req.chaddr, ea, Eaddrlen);
  427. if(filename != nil)
  428. strncpy(req.file, filename, sizeof(req.file));
  429. if(sysname != nil)
  430. strncpy(req.sname, sysname, sizeof(req.sname));
  431. myaddr.ip = 0;
  432. myaddr.port = BPportsrc;
  433. memmove(myaddr.ea, ea, Eaddrlen);
  434. rep = (Bootp*)rpkt;
  435. etherrxflush(ctlrno);
  436. for(i = 0; i < 20; i++) {
  437. server.ip = Bcastip;
  438. server.port = BPportdst;
  439. memmove(server.ea, broadcast, sizeof(server.ea));
  440. udpsend(ctlrno, &server, &req, sizeof(req));
  441. if(udprecv(ctlrno, &server, rpkt, sizeof(rpkt)) <= 0)
  442. continue;
  443. if(memcmp(req.chaddr, rep->chaddr, Eaddrlen))
  444. continue;
  445. if(rep->htype != 1 || rep->hlen != Eaddrlen)
  446. continue;
  447. if(sysname == 0 || strcmp(sysname, rep->sname) == 0)
  448. break;
  449. }
  450. if(i >= 20) {
  451. print("bootp on ether%d for %s timed out\n", ctlrno, file);
  452. return -1;
  453. }
  454. memmove(brep, rep, sizeof(Bootp));
  455. if(!dotftpopen)
  456. return 0;
  457. if(filename == 0 || *filename == 0){
  458. if(strcmp(rep->file, "/386/9pxeload") == 0)
  459. return -1;
  460. filename = rep->file;
  461. }
  462. if(rep->sname[0] != '\0')
  463. print("%s ", rep->sname);
  464. print("(%d.%d.%d.%d!%d): %s\n",
  465. rep->siaddr[0],
  466. rep->siaddr[1],
  467. rep->siaddr[2],
  468. rep->siaddr[3],
  469. server.port,
  470. filename);
  471. myaddr.ip = nhgetl(rep->yiaddr);
  472. myaddr.port = tftpport++;
  473. server.ip = nhgetl(rep->siaddr);
  474. server.port = TFTPport;
  475. if((n = tftpopen(ctlrno, &server, filename, &tftpb)) < 0)
  476. return -1;
  477. return n;
  478. }
  479. int
  480. bootpboot(int ctlrno, char *file, Boot *b)
  481. {
  482. int n;
  483. Bootp rep;
  484. if((n = bootpopen(ctlrno, file, &rep, 1)) < 0)
  485. return -1;
  486. while(n == TFTP_SEGSIZE && bootpass(b, tftpb.data, n) == MORE){
  487. n = tftpread(ctlrno, &server, &tftpb, sizeof(tftpb.data));
  488. if(n < sizeof(tftpb.data))
  489. break;
  490. }
  491. if(0 < n && n < sizeof(tftpb.data)) /* got to end of file */
  492. bootpass(b, tftpb.data, n);
  493. else
  494. nak(ctlrno, &server, 3, "ok", 0); /* tftpclose to abort transfer */
  495. bootpass(b, nil, 0); /* boot if possible */
  496. return -1;
  497. }
  498. #include "fs.h"
  499. #define INIPATHLEN 64
  500. static struct {
  501. Fs fs;
  502. char ini[INIPATHLEN];
  503. } pxether[MaxEther];
  504. static int64_t
  505. pxediskseek(Fs*, int64_t)
  506. {
  507. return -1LL;
  508. }
  509. static int32_t
  510. pxediskread(Fs*, void*, int32_t)
  511. {
  512. return -1;
  513. }
  514. static int32_t
  515. pxeread(File* f, void* va, int32_t len)
  516. {
  517. int n;
  518. Bootp rep;
  519. char *p, *v;
  520. if((n = bootpopen(f->fs->dev, pxether[f->fs->dev].ini, &rep, 1)) < 0)
  521. return -1;
  522. p = v = va;
  523. while(n > 0) {
  524. if((p-v)+n > len)
  525. n = len - (p-v);
  526. memmove(p, tftpb.data, n);
  527. p += n;
  528. if(n != TFTP_SEGSIZE)
  529. break;
  530. if((n = tftpread(f->fs->dev, &server, &tftpb, sizeof(tftpb.data))) < 0)
  531. return -1;
  532. }
  533. return p-v;
  534. }
  535. static int
  536. pxewalk(File* f, char* name)
  537. {
  538. char *ini;
  539. uint8_t *ea;
  540. switch(f->walked){
  541. default:
  542. return -1;
  543. case 0:
  544. if(strcmp(name, "cfg") == 0){
  545. f->walked = 1;
  546. return 1;
  547. }
  548. break;
  549. case 1:
  550. if(strcmp(name, "pxe") == 0){
  551. f->walked = 2;
  552. return 1;
  553. }
  554. break;
  555. case 2:
  556. if(strcmp(name, "%E") != 0)
  557. break;
  558. f->walked = 3;
  559. if((ea = etheraddr(f->fs->dev)) == 0){
  560. print("invalid ctlrno %d\n", f->fs->dev);
  561. return 0;
  562. }
  563. ini = pxether[f->fs->dev].ini;
  564. /* use our mac address instead of relying on a bootp answer */
  565. snprint(ini, INIPATHLEN, "/cfg/pxe/%E", ea);
  566. f->path = ini;
  567. return 1;
  568. }
  569. return 0;
  570. }
  571. void*
  572. pxegetfspart(int ctlrno, char* part, int)
  573. {
  574. if(!pxe)
  575. return nil;
  576. if(strcmp(part, "*") != 0)
  577. return nil;
  578. if(ctlrno >= MaxEther)
  579. return nil;
  580. if(iniread && getconf("*pxeini") != nil)
  581. return nil;
  582. pxether[ctlrno].fs.dev = ctlrno;
  583. pxether[ctlrno].fs.diskread = pxediskread;
  584. pxether[ctlrno].fs.diskseek = pxediskseek;
  585. pxether[ctlrno].fs.read = pxeread;
  586. pxether[ctlrno].fs.walk = pxewalk;
  587. pxether[ctlrno].fs.root.fs = &pxether[ctlrno].fs;
  588. pxether[ctlrno].fs.root.walked = 0;
  589. return &pxether[ctlrno].fs;
  590. }
  591. /*
  592. * hack to avoid queueing packets we don't care about.
  593. * needs to admit udp and aoe packets.
  594. */
  595. int
  596. interesting(void* data)
  597. {
  598. int len;
  599. uint32_t addr;
  600. Netaddr *a = &server;
  601. Udphdr *h;
  602. h = data;
  603. if(debug)
  604. print("inpkt %E to %E...\n", h->s, h->d);
  605. switch(nhgets(h->type)){
  606. case 0x88a2: /* AoE */
  607. return 1;
  608. case ET_IP:
  609. break;
  610. default:
  611. if(debug)
  612. print("not ipv4...");
  613. return 0;
  614. }
  615. if((h->vihl & 0xf0) != IP_VER) {
  616. if(debug)
  617. print("not ipv4 (%#4.4ux) %#2.2ux...",
  618. nhgets(h->type), h->vihl);
  619. return 0;
  620. }
  621. if((h->vihl & 0x0f) != IP_HLEN) {
  622. len = (h->vihl & 0xf)<<2;
  623. if(len < (IP_HLEN<<2)) {
  624. print("ip bad hlen %d %E to %E\n", len, h->s, h->d);
  625. return 0;
  626. }
  627. /*
  628. * fudge the header start,
  629. * can't strip the options here,
  630. * but only looking at udp stuff
  631. * from here on.
  632. */
  633. h = (Udphdr*)((uint8_t*)h + (len - (IP_HLEN<<2)));
  634. if(debug)
  635. print("fudge %d\n", len);
  636. }
  637. if(h->udpproto != IP_UDPPROTO) {
  638. if(debug)
  639. print("not udp (%d)...", h->udpproto);
  640. return 0;
  641. }
  642. if(debug)
  643. print("okay udp...");
  644. if(a->port != 0 && nhgets(h->udpsport) != a->port) {
  645. if(debug)
  646. print("udpport %ux not %ux\n",
  647. nhgets(h->udpsport), a->port);
  648. return 0;
  649. }
  650. addr = nhgetl(h->udpsrc);
  651. if(a->ip != Bcastip && a->ip != addr) {
  652. if(debug)
  653. print("bad ip %lux not %lux\n", addr, a->ip);
  654. return 0;
  655. }
  656. return 1;
  657. }