pcap-dlpi.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. /*
  2. * Copyright (c) 1993, 1994, 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
  22. * University College London, and subsequently modified by
  23. * Guy Harris (guy@alum.mit.edu), Mark Pizzolato
  24. * <List-tcpdump-workers@subscriptions.pizzolato.net>,
  25. * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
  26. */
  27. /*
  28. * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
  29. *
  30. * Notes:
  31. *
  32. * - The DLIOCRAW ioctl() is specific to SunOS.
  33. *
  34. * - There is a bug in bufmod(7) such that setting the snapshot
  35. * length results in data being left of the front of the packet.
  36. *
  37. * - It might be desirable to use pfmod(7) to filter packets in the
  38. * kernel when possible.
  39. *
  40. * - An older version of the HP-UX DLPI Programmer's Guide, which
  41. * I think was advertised as the 10.20 version, used to be available
  42. * at
  43. *
  44. * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html
  45. *
  46. * but is no longer available; it can still be found at
  47. *
  48. * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf
  49. *
  50. * in PDF form.
  51. *
  52. * - The HP-UX 10.x, 11.0, and 11i v1.6 version of the HP-UX DLPI
  53. * Programmer's Guide, which I think was once advertised as the
  54. * 11.00 version is available at
  55. *
  56. * http://docs.hp.com/en/B2355-90139/index.html
  57. *
  58. * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide
  59. * is available at
  60. *
  61. * http://docs.hp.com/en/B2355-90871/index.html
  62. *
  63. * - All of the HP documents describe raw-mode services, which are
  64. * what we use if DL_HP_RAWDLS is defined. XXX - we use __hpux
  65. * in some places to test for HP-UX, but use DL_HP_RAWDLS in
  66. * other places; do we support any versions of HP-UX without
  67. * DL_HP_RAWDLS?
  68. */
  69. #ifdef HAVE_CONFIG_H
  70. #include <config.h>
  71. #endif
  72. #include <sys/types.h>
  73. #include <sys/time.h>
  74. #ifdef HAVE_SYS_BUFMOD_H
  75. #include <sys/bufmod.h>
  76. #endif
  77. #include <sys/dlpi.h>
  78. #ifdef HAVE_SYS_DLPI_EXT_H
  79. #include <sys/dlpi_ext.h>
  80. #endif
  81. #ifdef HAVE_HPUX9
  82. #include <sys/socket.h>
  83. #endif
  84. #ifdef DL_HP_PPA_REQ
  85. #include <sys/stat.h>
  86. #endif
  87. #include <sys/stream.h>
  88. #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
  89. #include <sys/systeminfo.h>
  90. #endif
  91. #ifdef HAVE_HPUX9
  92. #include <net/if.h>
  93. #endif
  94. #include <ctype.h>
  95. #ifdef HAVE_HPUX9
  96. #include <nlist.h>
  97. #endif
  98. #include <errno.h>
  99. #include <fcntl.h>
  100. #include <memory.h>
  101. #include <stdio.h>
  102. #include <stdlib.h>
  103. #include <string.h>
  104. #include <stropts.h>
  105. #include <unistd.h>
  106. #ifdef HAVE_LIMITS_H
  107. #include <limits.h>
  108. #else
  109. #define INT_MAX 2147483647
  110. #endif
  111. #include "pcap-int.h"
  112. #include "dlpisubs.h"
  113. #ifdef HAVE_OS_PROTO_H
  114. #include "os-proto.h"
  115. #endif
  116. #if defined(__hpux)
  117. /*
  118. * HP-UX has a /dev/dlpi device; you open it and set the PPA of the actual
  119. * network device you want.
  120. */
  121. #define HAVE_DEV_DLPI
  122. #elif defined(_AIX)
  123. /*
  124. * AIX has a /dev/dlpi directory, with devices named after the interfaces
  125. * underneath it.
  126. */
  127. #define PCAP_DEV_PREFIX "/dev/dlpi"
  128. #elif defined(HAVE_SOLARIS)
  129. /*
  130. * Solaris has devices named after the interfaces underneath /dev.
  131. */
  132. #define PCAP_DEV_PREFIX "/dev"
  133. #endif
  134. #define MAXDLBUF 8192
  135. /* Forwards */
  136. static char *split_dname(char *, u_int *, char *);
  137. static int dl_doattach(int, int, char *);
  138. #ifdef DL_HP_RAWDLS
  139. static int dl_dohpuxbind(int, char *);
  140. #endif
  141. static int dlpromiscon(pcap_t *, bpf_u_int32);
  142. static int dlbindreq(int, bpf_u_int32, char *);
  143. static int dlbindack(int, char *, char *, int *);
  144. static int dlokack(int, const char *, char *, char *);
  145. static int dlinforeq(int, char *);
  146. static int dlinfoack(int, char *, char *);
  147. #ifdef HAVE_DL_PASSIVE_REQ_T
  148. static void dlpassive(int, char *);
  149. #endif
  150. #ifdef DL_HP_RAWDLS
  151. static int dlrawdatareq(int, const u_char *, int);
  152. #endif
  153. static int recv_ack(int, int, const char *, char *, char *, int *);
  154. static char *dlstrerror(char *, size_t, bpf_u_int32);
  155. static char *dlprim(char *, size_t, bpf_u_int32);
  156. #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
  157. #define GET_RELEASE_BUFSIZE 32
  158. static void get_release(char *, size_t, bpf_u_int32 *, bpf_u_int32 *,
  159. bpf_u_int32 *);
  160. #endif
  161. static int send_request(int, char *, int, char *, char *);
  162. #ifdef HAVE_HPUX9
  163. static int dlpi_kread(int, off_t, void *, u_int, char *);
  164. #endif
  165. #ifdef HAVE_DEV_DLPI
  166. static int get_dlpi_ppa(int, const char *, u_int, u_int *, char *);
  167. #endif
  168. /*
  169. * Cast a buffer to "union DL_primitives" without provoking warnings
  170. * from the compiler.
  171. */
  172. #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr))
  173. static int
  174. pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  175. {
  176. int cc;
  177. u_char *bp;
  178. int flags;
  179. bpf_u_int32 ctlbuf[MAXDLBUF];
  180. struct strbuf ctl = {
  181. MAXDLBUF,
  182. 0,
  183. (char *)ctlbuf
  184. };
  185. struct strbuf data;
  186. flags = 0;
  187. cc = p->cc;
  188. if (cc == 0) {
  189. data.buf = (char *)p->buffer + p->offset;
  190. data.maxlen = p->bufsize;
  191. data.len = 0;
  192. do {
  193. /*
  194. * Has "pcap_breakloop()" been called?
  195. */
  196. if (p->break_loop) {
  197. /*
  198. * Yes - clear the flag that indicates
  199. * that it has, and return -2 to
  200. * indicate that we were told to
  201. * break out of the loop.
  202. */
  203. p->break_loop = 0;
  204. return (-2);
  205. }
  206. /*
  207. * XXX - check for the DLPI primitive, which
  208. * would be DL_HP_RAWDATA_IND on HP-UX
  209. * if we're in raw mode?
  210. */
  211. ctl.buf = (char *)ctlbuf;
  212. ctl.maxlen = MAXDLBUF;
  213. ctl.len = 0;
  214. if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
  215. /* Don't choke when we get ptraced */
  216. switch (errno) {
  217. case EINTR:
  218. cc = 0;
  219. continue;
  220. case EAGAIN:
  221. return (0);
  222. }
  223. pcap_fmt_errmsg_for_errno(p->errbuf,
  224. sizeof(p->errbuf), errno, "getmsg");
  225. return (-1);
  226. }
  227. cc = data.len;
  228. } while (cc == 0);
  229. bp = (u_char *)p->buffer + p->offset;
  230. } else
  231. bp = p->bp;
  232. return (pcap_process_pkts(p, callback, user, cnt, bp, cc));
  233. }
  234. static int
  235. pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size)
  236. {
  237. #ifdef DL_HP_RAWDLS
  238. struct pcap_dlpi *pd = p->priv;
  239. #endif
  240. int ret;
  241. #if defined(DLIOCRAW)
  242. ret = write(p->fd, buf, size);
  243. if (ret == -1) {
  244. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  245. errno, "send");
  246. return (-1);
  247. }
  248. #elif defined(DL_HP_RAWDLS)
  249. if (pd->send_fd < 0) {
  250. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  251. "send: Output FD couldn't be opened");
  252. return (-1);
  253. }
  254. ret = dlrawdatareq(pd->send_fd, buf, size);
  255. if (ret == -1) {
  256. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  257. errno, "send");
  258. return (-1);
  259. }
  260. /*
  261. * putmsg() returns either 0 or -1; it doesn't indicate how
  262. * many bytes were written (presumably they were all written
  263. * or none of them were written). OpenBSD's pcap_inject()
  264. * returns the number of bytes written, so, for API compatibility,
  265. * we return the number of bytes we were told to write.
  266. */
  267. ret = size;
  268. #else /* no raw mode */
  269. /*
  270. * XXX - this is a pain, because you might have to extract
  271. * the address from the packet and use it in a DL_UNITDATA_REQ
  272. * request. That would be dependent on the link-layer type.
  273. *
  274. * I also don't know what SAP you'd have to bind the descriptor
  275. * to, or whether you'd need separate "receive" and "send" FDs,
  276. * nor do I know whether you'd need different bindings for
  277. * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
  278. * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
  279. *
  280. * So, for now, we just return a "you can't send" indication,
  281. * and leave it up to somebody with a DLPI-based system lacking
  282. * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
  283. * packet transmission on that system. If they do, they should
  284. * send it to us - but should not send us code that assumes
  285. * Ethernet; if the code doesn't work on non-Ethernet interfaces,
  286. * it should check "p->linktype" and reject the send request if
  287. * it's anything other than DLT_EN10MB.
  288. */
  289. strlcpy(p->errbuf, "send: Not supported on this version of this OS",
  290. PCAP_ERRBUF_SIZE);
  291. ret = -1;
  292. #endif /* raw mode */
  293. return (ret);
  294. }
  295. #ifndef DL_IPATM
  296. #define DL_IPATM 0x12 /* ATM Classical IP interface */
  297. #endif
  298. #ifdef HAVE_SOLARIS
  299. /*
  300. * For SunATM.
  301. */
  302. #ifndef A_GET_UNITS
  303. #define A_GET_UNITS (('A'<<8)|118)
  304. #endif /* A_GET_UNITS */
  305. #ifndef A_PROMISCON_REQ
  306. #define A_PROMISCON_REQ (('A'<<8)|121)
  307. #endif /* A_PROMISCON_REQ */
  308. #endif /* HAVE_SOLARIS */
  309. static void
  310. pcap_cleanup_dlpi(pcap_t *p)
  311. {
  312. #ifdef DL_HP_RAWDLS
  313. struct pcap_dlpi *pd = p->priv;
  314. if (pd->send_fd >= 0) {
  315. close(pd->send_fd);
  316. pd->send_fd = -1;
  317. }
  318. #endif
  319. pcap_cleanup_live_common(p);
  320. }
  321. static int
  322. open_dlpi_device(const char *name, u_int *ppa, char *errbuf)
  323. {
  324. int status;
  325. char dname[100];
  326. char *cp;
  327. int fd;
  328. #ifdef HAVE_DEV_DLPI
  329. u_int unit;
  330. #else
  331. char dname2[100];
  332. #endif
  333. #ifdef HAVE_DEV_DLPI
  334. /*
  335. ** Remove any "/dev/" on the front of the device.
  336. */
  337. cp = strrchr(name, '/');
  338. if (cp == NULL)
  339. strlcpy(dname, name, sizeof(dname));
  340. else
  341. strlcpy(dname, cp + 1, sizeof(dname));
  342. /*
  343. * Split the device name into a device type name and a unit number;
  344. * chop off the unit number, so "dname" is just a device type name.
  345. */
  346. cp = split_dname(dname, &unit, errbuf);
  347. if (cp == NULL)
  348. return (PCAP_ERROR_NO_SUCH_DEVICE);
  349. *cp = '\0';
  350. /*
  351. * Use "/dev/dlpi" as the device.
  352. *
  353. * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
  354. * the "dl_mjr_num" field is for the "major number of interface
  355. * driver"; that's the major of "/dev/dlpi" on the system on
  356. * which I tried this, but there may be DLPI devices that
  357. * use a different driver, in which case we may need to
  358. * search "/dev" for the appropriate device with that major
  359. * device number, rather than hardwiring "/dev/dlpi".
  360. */
  361. cp = "/dev/dlpi";
  362. if ((fd = open(cp, O_RDWR)) < 0) {
  363. if (errno == EPERM || errno == EACCES)
  364. status = PCAP_ERROR_PERM_DENIED;
  365. else
  366. status = PCAP_ERROR;
  367. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  368. errno, "%s", cp);
  369. return (status);
  370. }
  371. /*
  372. * Get a table of all PPAs for that device, and search that
  373. * table for the specified device type name and unit number.
  374. */
  375. status = get_dlpi_ppa(fd, dname, unit, ppa, errbuf);
  376. if (status < 0) {
  377. close(fd);
  378. return (status);
  379. }
  380. #else
  381. /*
  382. * If the device name begins with "/", assume it begins with
  383. * the pathname of the directory containing the device to open;
  384. * otherwise, concatenate the device directory name and the
  385. * device name.
  386. */
  387. if (*name == '/')
  388. strlcpy(dname, name, sizeof(dname));
  389. else
  390. pcap_snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
  391. name);
  392. /*
  393. * Get the unit number, and a pointer to the end of the device
  394. * type name.
  395. */
  396. cp = split_dname(dname, ppa, errbuf);
  397. if (cp == NULL)
  398. return (PCAP_ERROR_NO_SUCH_DEVICE);
  399. /*
  400. * Make a copy of the device pathname, and then remove the unit
  401. * number from the device pathname.
  402. */
  403. strlcpy(dname2, dname, sizeof(dname));
  404. *cp = '\0';
  405. /* Try device without unit number */
  406. if ((fd = open(dname, O_RDWR)) < 0) {
  407. if (errno != ENOENT) {
  408. if (errno == EPERM || errno == EACCES)
  409. status = PCAP_ERROR_PERM_DENIED;
  410. else
  411. status = PCAP_ERROR;
  412. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  413. errno, "%s", dname);
  414. return (status);
  415. }
  416. /* Try again with unit number */
  417. if ((fd = open(dname2, O_RDWR)) < 0) {
  418. if (errno == ENOENT) {
  419. status = PCAP_ERROR_NO_SUCH_DEVICE;
  420. /*
  421. * We provide an error message even
  422. * for this error, for diagnostic
  423. * purposes (so that, for example,
  424. * the app can show the message if the
  425. * user requests it).
  426. *
  427. * In it, we just report "No DLPI device
  428. * found" with the device name, so people
  429. * don't get confused and think, for example,
  430. * that if they can't capture on "lo0"
  431. * on Solaris prior to Solaris 11 the fix
  432. * is to change libpcap (or the application
  433. * that uses it) to look for something other
  434. * than "/dev/lo0", as the fix is to use
  435. * Solaris 11 or some operating system
  436. * other than Solaris - you just *can't*
  437. * capture on a loopback interface
  438. * on Solaris prior to Solaris 11, the lack
  439. * of a DLPI device for the loopback
  440. * interface is just a symptom of that
  441. * inability.
  442. */
  443. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  444. "%s: No DLPI device found", name);
  445. } else {
  446. if (errno == EPERM || errno == EACCES)
  447. status = PCAP_ERROR_PERM_DENIED;
  448. else
  449. status = PCAP_ERROR;
  450. pcap_fmt_errmsg_for_errno(errbuf,
  451. PCAP_ERRBUF_SIZE, errno, "%s", dname2);
  452. }
  453. return (status);
  454. }
  455. /* XXX Assume unit zero */
  456. *ppa = 0;
  457. }
  458. #endif
  459. return (fd);
  460. }
  461. static int
  462. pcap_activate_dlpi(pcap_t *p)
  463. {
  464. #ifdef DL_HP_RAWDLS
  465. struct pcap_dlpi *pd = p->priv;
  466. #endif
  467. int status = 0;
  468. int retv;
  469. u_int ppa;
  470. #ifdef HAVE_SOLARIS
  471. int isatm = 0;
  472. #endif
  473. register dl_info_ack_t *infop;
  474. #ifdef HAVE_SYS_BUFMOD_H
  475. bpf_u_int32 ss;
  476. #ifdef HAVE_SOLARIS
  477. char release[GET_RELEASE_BUFSIZE];
  478. bpf_u_int32 osmajor, osminor, osmicro;
  479. #endif
  480. #endif
  481. bpf_u_int32 buf[MAXDLBUF];
  482. p->fd = open_dlpi_device(p->opt.device, &ppa, p->errbuf);
  483. if (p->fd < 0) {
  484. status = p->fd;
  485. goto bad;
  486. }
  487. #ifdef DL_HP_RAWDLS
  488. /*
  489. * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
  490. * receiving packets on the same descriptor - you need separate
  491. * descriptors for sending and receiving, bound to different SAPs.
  492. *
  493. * If the open fails, we just leave -1 in "pd->send_fd" and reject
  494. * attempts to send packets, just as if, in pcap-bpf.c, we fail
  495. * to open the BPF device for reading and writing, we just try
  496. * to open it for reading only and, if that succeeds, just let
  497. * the send attempts fail.
  498. */
  499. pd->send_fd = open("/dev/dlpi", O_RDWR);
  500. #endif
  501. /*
  502. ** Attach if "style 2" provider
  503. */
  504. if (dlinforeq(p->fd, p->errbuf) < 0 ||
  505. dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
  506. status = PCAP_ERROR;
  507. goto bad;
  508. }
  509. infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
  510. #ifdef HAVE_SOLARIS
  511. if (infop->dl_mac_type == DL_IPATM)
  512. isatm = 1;
  513. #endif
  514. if (infop->dl_provider_style == DL_STYLE2) {
  515. retv = dl_doattach(p->fd, ppa, p->errbuf);
  516. if (retv < 0) {
  517. status = retv;
  518. goto bad;
  519. }
  520. #ifdef DL_HP_RAWDLS
  521. if (pd->send_fd >= 0) {
  522. retv = dl_doattach(pd->send_fd, ppa, p->errbuf);
  523. if (retv < 0) {
  524. status = retv;
  525. goto bad;
  526. }
  527. }
  528. #endif
  529. }
  530. if (p->opt.rfmon) {
  531. /*
  532. * This device exists, but we don't support monitor mode
  533. * any platforms that support DLPI.
  534. */
  535. status = PCAP_ERROR_RFMON_NOTSUP;
  536. goto bad;
  537. }
  538. #ifdef HAVE_DL_PASSIVE_REQ_T
  539. /*
  540. * Enable Passive mode to be able to capture on aggregated link.
  541. * Not supported in all Solaris versions.
  542. */
  543. dlpassive(p->fd, p->errbuf);
  544. #endif
  545. /*
  546. ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
  547. ** skip if using SINIX)
  548. */
  549. #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
  550. #ifdef _AIX
  551. /*
  552. ** AIX.
  553. ** According to IBM's AIX Support Line, the dl_sap value
  554. ** should not be less than 0x600 (1536) for standard Ethernet.
  555. ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
  556. ** format or invalid" - errors if we use 1537 on the "tr0"
  557. ** device, which, given that its name starts with "tr" and that
  558. ** it's IBM, probably means a Token Ring device. (Perhaps we
  559. ** need to use 1537 on "/dev/dlpi/en" because that device is for
  560. ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
  561. ** it rejects invalid Ethernet types.)
  562. **
  563. ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
  564. ** says that works on Token Ring (he says that 0 does *not*
  565. ** work; perhaps that's considered an invalid LLC SAP value - I
  566. ** assume the SAP value in a DLPI bind is an LLC SAP for network
  567. ** types that use 802.2 LLC).
  568. */
  569. if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 &&
  570. dlbindreq(p->fd, 2, p->errbuf) < 0) ||
  571. dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
  572. status = PCAP_ERROR;
  573. goto bad;
  574. }
  575. #elif defined(DL_HP_RAWDLS)
  576. /*
  577. ** HP-UX 10.0x and 10.1x.
  578. */
  579. if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
  580. status = PCAP_ERROR;
  581. goto bad;
  582. }
  583. if (pd->send_fd >= 0) {
  584. /*
  585. ** XXX - if this fails, just close send_fd and
  586. ** set it to -1, so that you can't send but can
  587. ** still receive?
  588. */
  589. if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
  590. status = PCAP_ERROR;
  591. goto bad;
  592. }
  593. }
  594. #else /* neither AIX nor HP-UX */
  595. /*
  596. ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
  597. ** OS using DLPI.
  598. **/
  599. if (dlbindreq(p->fd, 0, p->errbuf) < 0 ||
  600. dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
  601. status = PCAP_ERROR;
  602. goto bad;
  603. }
  604. #endif /* AIX vs. HP-UX vs. other */
  605. #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
  606. /*
  607. * Turn a negative snapshot value (invalid), a snapshot value of
  608. * 0 (unspecified), or a value bigger than the normal maximum
  609. * value, into the maximum allowed value.
  610. *
  611. * If some application really *needs* a bigger snapshot
  612. * length, we should just increase MAXIMUM_SNAPLEN.
  613. */
  614. if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
  615. p->snapshot = MAXIMUM_SNAPLEN;
  616. #ifdef HAVE_SOLARIS
  617. if (isatm) {
  618. /*
  619. ** Have to turn on some special ATM promiscuous mode
  620. ** for SunATM.
  621. ** Do *NOT* turn regular promiscuous mode on; it doesn't
  622. ** help, and may break things.
  623. */
  624. if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
  625. status = PCAP_ERROR;
  626. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  627. errno, "A_PROMISCON_REQ");
  628. goto bad;
  629. }
  630. } else
  631. #endif
  632. if (p->opt.promisc) {
  633. /*
  634. ** Enable promiscuous (not necessary on send FD)
  635. */
  636. retv = dlpromiscon(p, DL_PROMISC_PHYS);
  637. if (retv < 0) {
  638. if (retv == PCAP_ERROR_PERM_DENIED)
  639. status = PCAP_ERROR_PROMISC_PERM_DENIED;
  640. else
  641. status = retv;
  642. goto bad;
  643. }
  644. /*
  645. ** Try to enable multicast (you would have thought
  646. ** promiscuous would be sufficient). (Skip if using
  647. ** HP-UX or SINIX) (Not necessary on send FD)
  648. */
  649. #if !defined(__hpux) && !defined(sinix)
  650. retv = dlpromiscon(p, DL_PROMISC_MULTI);
  651. if (retv < 0)
  652. status = PCAP_WARNING;
  653. #endif
  654. }
  655. /*
  656. ** Try to enable SAP promiscuity (when not in promiscuous mode
  657. ** when using HP-UX, when not doing SunATM on Solaris, and never
  658. ** under SINIX) (Not necessary on send FD)
  659. */
  660. #ifndef sinix
  661. #if defined(__hpux)
  662. /* HP-UX - only do this when not in promiscuous mode */
  663. if (!p->opt.promisc) {
  664. #elif defined(HAVE_SOLARIS)
  665. /* Solaris - don't do this on SunATM devices */
  666. if (!isatm) {
  667. #else
  668. /* Everything else (except for SINIX) - always do this */
  669. {
  670. #endif
  671. retv = dlpromiscon(p, DL_PROMISC_SAP);
  672. if (retv < 0) {
  673. if (p->opt.promisc) {
  674. /*
  675. * Not fatal, since the DL_PROMISC_PHYS mode
  676. * worked.
  677. *
  678. * Report it as a warning, however.
  679. */
  680. status = PCAP_WARNING;
  681. } else {
  682. /*
  683. * Fatal.
  684. */
  685. status = retv;
  686. goto bad;
  687. }
  688. }
  689. }
  690. #endif /* sinix */
  691. /*
  692. ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
  693. ** promiscuous options.
  694. */
  695. #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
  696. if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
  697. status = PCAP_ERROR;
  698. goto bad;
  699. }
  700. /*
  701. ** We don't set promiscuous mode on the send FD, but we'll defer
  702. ** binding it anyway, just to keep the HP-UX 9/10.20 or later
  703. ** code together.
  704. */
  705. if (pd->send_fd >= 0) {
  706. /*
  707. ** XXX - if this fails, just close send_fd and
  708. ** set it to -1, so that you can't send but can
  709. ** still receive?
  710. */
  711. if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
  712. status = PCAP_ERROR;
  713. goto bad;
  714. }
  715. }
  716. #endif
  717. /*
  718. ** Determine link type
  719. ** XXX - get SAP length and address length as well, for use
  720. ** when sending packets.
  721. */
  722. if (dlinforeq(p->fd, p->errbuf) < 0 ||
  723. dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
  724. status = PCAP_ERROR;
  725. goto bad;
  726. }
  727. infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
  728. if (pcap_process_mactype(p, infop->dl_mac_type) != 0) {
  729. status = PCAP_ERROR;
  730. goto bad;
  731. }
  732. #ifdef DLIOCRAW
  733. /*
  734. ** This is a non standard SunOS hack to get the full raw link-layer
  735. ** header.
  736. */
  737. if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
  738. status = PCAP_ERROR;
  739. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  740. errno, "DLIOCRAW");
  741. goto bad;
  742. }
  743. #endif
  744. #ifdef HAVE_SYS_BUFMOD_H
  745. ss = p->snapshot;
  746. /*
  747. ** There is a bug in bufmod(7). When dealing with messages of
  748. ** less than snaplen size it strips data from the beginning not
  749. ** the end.
  750. **
  751. ** This bug is fixed in 5.3.2. Also, there is a patch available.
  752. ** Ask for bugid 1149065.
  753. */
  754. #ifdef HAVE_SOLARIS
  755. get_release(release, sizeof (release), &osmajor, &osminor, &osmicro);
  756. if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
  757. getenv("BUFMOD_FIXED") == NULL) {
  758. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  759. "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
  760. release);
  761. ss = 0;
  762. status = PCAP_WARNING;
  763. }
  764. #endif
  765. /* Push and configure bufmod. */
  766. if (pcap_conf_bufmod(p, ss) != 0) {
  767. status = PCAP_ERROR;
  768. goto bad;
  769. }
  770. #endif
  771. /*
  772. ** As the last operation flush the read side.
  773. */
  774. if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
  775. status = PCAP_ERROR;
  776. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  777. errno, "FLUSHR");
  778. goto bad;
  779. }
  780. /* Allocate data buffer. */
  781. if (pcap_alloc_databuf(p) != 0) {
  782. status = PCAP_ERROR;
  783. goto bad;
  784. }
  785. /*
  786. * Success.
  787. *
  788. * "p->fd" is an FD for a STREAMS device, so "select()" and
  789. * "poll()" should work on it.
  790. */
  791. p->selectable_fd = p->fd;
  792. p->read_op = pcap_read_dlpi;
  793. p->inject_op = pcap_inject_dlpi;
  794. p->setfilter_op = install_bpf_program; /* no kernel filtering */
  795. p->setdirection_op = NULL; /* Not implemented.*/
  796. p->set_datalink_op = NULL; /* can't change data link type */
  797. p->getnonblock_op = pcap_getnonblock_fd;
  798. p->setnonblock_op = pcap_setnonblock_fd;
  799. p->stats_op = pcap_stats_dlpi;
  800. p->cleanup_op = pcap_cleanup_dlpi;
  801. return (status);
  802. bad:
  803. pcap_cleanup_dlpi(p);
  804. return (status);
  805. }
  806. /*
  807. * Split a device name into a device type name and a unit number;
  808. * return the a pointer to the beginning of the unit number, which
  809. * is the end of the device type name, and set "*unitp" to the unit
  810. * number.
  811. *
  812. * Returns NULL on error, and fills "ebuf" with an error message.
  813. */
  814. static char *
  815. split_dname(char *device, u_int *unitp, char *ebuf)
  816. {
  817. char *cp;
  818. char *eos;
  819. long unit;
  820. /*
  821. * Look for a number at the end of the device name string.
  822. */
  823. cp = device + strlen(device) - 1;
  824. if (*cp < '0' || *cp > '9') {
  825. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
  826. device);
  827. return (NULL);
  828. }
  829. /* Digits at end of string are unit number */
  830. while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
  831. cp--;
  832. errno = 0;
  833. unit = strtol(cp, &eos, 10);
  834. if (*eos != '\0') {
  835. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
  836. return (NULL);
  837. }
  838. if (errno == ERANGE || unit > INT_MAX) {
  839. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
  840. device);
  841. return (NULL);
  842. }
  843. if (unit < 0) {
  844. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
  845. device);
  846. return (NULL);
  847. }
  848. *unitp = (u_int)unit;
  849. return (cp);
  850. }
  851. static int
  852. dl_doattach(int fd, int ppa, char *ebuf)
  853. {
  854. dl_attach_req_t req;
  855. bpf_u_int32 buf[MAXDLBUF];
  856. int err;
  857. req.dl_primitive = DL_ATTACH_REQ;
  858. req.dl_ppa = ppa;
  859. if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
  860. return (PCAP_ERROR);
  861. err = dlokack(fd, "attach", (char *)buf, ebuf);
  862. if (err < 0)
  863. return (err);
  864. return (0);
  865. }
  866. #ifdef DL_HP_RAWDLS
  867. static int
  868. dl_dohpuxbind(int fd, char *ebuf)
  869. {
  870. int hpsap;
  871. int uerror;
  872. bpf_u_int32 buf[MAXDLBUF];
  873. /*
  874. * XXX - we start at 22 because we used to use only 22, but
  875. * that was just because that was the value used in some
  876. * sample code from HP. With what value *should* we start?
  877. * Does it matter, given that we're enabling SAP promiscuity
  878. * on the input FD?
  879. */
  880. hpsap = 22;
  881. for (;;) {
  882. if (dlbindreq(fd, hpsap, ebuf) < 0)
  883. return (-1);
  884. if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
  885. break;
  886. /*
  887. * For any error other than a UNIX EBUSY, give up.
  888. */
  889. if (uerror != EBUSY) {
  890. /*
  891. * dlbindack() has already filled in ebuf for
  892. * this error.
  893. */
  894. return (-1);
  895. }
  896. /*
  897. * For EBUSY, try the next SAP value; that means that
  898. * somebody else is using that SAP. Clear ebuf so
  899. * that application doesn't report the "Device busy"
  900. * error as a warning.
  901. */
  902. *ebuf = '\0';
  903. hpsap++;
  904. if (hpsap > 100) {
  905. strlcpy(ebuf,
  906. "All SAPs from 22 through 100 are in use",
  907. PCAP_ERRBUF_SIZE);
  908. return (-1);
  909. }
  910. }
  911. return (0);
  912. }
  913. #endif
  914. #define STRINGIFY(n) #n
  915. static int
  916. dlpromiscon(pcap_t *p, bpf_u_int32 level)
  917. {
  918. dl_promiscon_req_t req;
  919. bpf_u_int32 buf[MAXDLBUF];
  920. int err;
  921. req.dl_primitive = DL_PROMISCON_REQ;
  922. req.dl_level = level;
  923. if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
  924. p->errbuf) < 0)
  925. return (PCAP_ERROR);
  926. err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
  927. p->errbuf);
  928. if (err < 0)
  929. return (err);
  930. return (0);
  931. }
  932. /*
  933. * Not all interfaces are DLPI interfaces, and thus not all interfaces
  934. * can be opened with DLPI (for example, the loopback interface is not
  935. * a DLPI interface on Solaris prior to Solaris 11), so try to open
  936. * the specified interface; return 0 if we fail with PCAP_ERROR_NO_SUCH_DEVICE
  937. * and 1 otherwise.
  938. */
  939. static int
  940. is_dlpi_interface(const char *name)
  941. {
  942. int fd;
  943. u_int ppa;
  944. char errbuf[PCAP_ERRBUF_SIZE];
  945. fd = open_dlpi_device(name, &ppa, errbuf);
  946. if (fd < 0) {
  947. /*
  948. * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
  949. */
  950. if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
  951. /*
  952. * Yes, so we can't open this because it's
  953. * not a DLPI interface.
  954. */
  955. return (0);
  956. }
  957. /*
  958. * No, so, in the case where there's a single DLPI
  959. * device for all interfaces of this type ("style
  960. * 2" providers?), we don't know whether it's a DLPI
  961. * interface or not, as we didn't try an attach.
  962. * Say it is a DLPI device, so that the user can at
  963. * least try to open it and report the error (which
  964. * is probably "you don't have permission to open that
  965. * DLPI device"; reporting those interfaces means
  966. * users will ask "why am I getting a permissions error
  967. * when I try to capture" rather than "why am I not
  968. * seeing any interfaces", making the underlying problem
  969. * clearer).
  970. */
  971. return (1);
  972. }
  973. /*
  974. * Success.
  975. */
  976. close(fd);
  977. return (1);
  978. }
  979. static int
  980. get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
  981. {
  982. /*
  983. * Nothing we can do other than mark loopback devices as "the
  984. * connected/disconnected status doesn't apply".
  985. *
  986. * XXX - on Solaris, can we do what the dladm command does,
  987. * i.e. get a connected/disconnected indication from a kstat?
  988. * (Note that you can also get the link speed, and possibly
  989. * other information, from a kstat as well.)
  990. */
  991. if (*flags & PCAP_IF_LOOPBACK) {
  992. /*
  993. * Loopback devices aren't wireless, and "connected"/
  994. * "disconnected" doesn't apply to them.
  995. */
  996. *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
  997. return (0);
  998. }
  999. return (0);
  1000. }
  1001. int
  1002. pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
  1003. {
  1004. #ifdef HAVE_SOLARIS
  1005. int fd;
  1006. union {
  1007. u_int nunits;
  1008. char pad[516]; /* XXX - must be at least 513; is 516
  1009. in "atmgetunits" */
  1010. } buf;
  1011. char baname[2+1+1];
  1012. u_int i;
  1013. #endif
  1014. /*
  1015. * Get the list of regular interfaces first.
  1016. */
  1017. if (pcap_findalldevs_interfaces(devlistp, errbuf, is_dlpi_interface,
  1018. get_if_flags) == -1)
  1019. return (-1); /* failure */
  1020. #ifdef HAVE_SOLARIS
  1021. /*
  1022. * We may have to do special magic to get ATM devices.
  1023. */
  1024. if ((fd = open("/dev/ba", O_RDWR)) < 0) {
  1025. /*
  1026. * We couldn't open the "ba" device.
  1027. * For now, just give up; perhaps we should
  1028. * return an error if the problem is neither
  1029. * a "that device doesn't exist" error (ENOENT,
  1030. * ENXIO, etc.) or a "you're not allowed to do
  1031. * that" error (EPERM, EACCES).
  1032. */
  1033. return (0);
  1034. }
  1035. if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
  1036. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1037. errno, "A_GET_UNITS");
  1038. return (-1);
  1039. }
  1040. for (i = 0; i < buf.nunits; i++) {
  1041. pcap_snprintf(baname, sizeof baname, "ba%u", i);
  1042. /*
  1043. * XXX - is there a notion of "up" and "running"?
  1044. * And is there a way to determine whether the
  1045. * interface is plugged into a network?
  1046. */
  1047. if (add_dev(devlistp, baname, 0, NULL, errbuf) == NULL)
  1048. return (-1);
  1049. }
  1050. #endif
  1051. return (0);
  1052. }
  1053. static int
  1054. send_request(int fd, char *ptr, int len, char *what, char *ebuf)
  1055. {
  1056. struct strbuf ctl;
  1057. int flags;
  1058. ctl.maxlen = 0;
  1059. ctl.len = len;
  1060. ctl.buf = ptr;
  1061. flags = 0;
  1062. if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
  1063. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1064. errno, "send_request: putmsg \"%s\"", what);
  1065. return (-1);
  1066. }
  1067. return (0);
  1068. }
  1069. static int
  1070. recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
  1071. {
  1072. union DL_primitives *dlp;
  1073. struct strbuf ctl;
  1074. int flags;
  1075. char errmsgbuf[PCAP_ERRBUF_SIZE];
  1076. char dlprimbuf[64];
  1077. /*
  1078. * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
  1079. * making that the only place where EBUSY is treated specially.
  1080. */
  1081. if (uerror != NULL)
  1082. *uerror = 0;
  1083. ctl.maxlen = MAXDLBUF;
  1084. ctl.len = 0;
  1085. ctl.buf = bufp;
  1086. flags = 0;
  1087. if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
  1088. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1089. errno, "recv_ack: %s getmsg", what);
  1090. return (PCAP_ERROR);
  1091. }
  1092. dlp = MAKE_DL_PRIMITIVES(ctl.buf);
  1093. switch (dlp->dl_primitive) {
  1094. case DL_INFO_ACK:
  1095. case DL_BIND_ACK:
  1096. case DL_OK_ACK:
  1097. #ifdef DL_HP_PPA_ACK
  1098. case DL_HP_PPA_ACK:
  1099. #endif
  1100. /* These are OK */
  1101. break;
  1102. case DL_ERROR_ACK:
  1103. switch (dlp->error_ack.dl_errno) {
  1104. case DL_SYSERR:
  1105. if (uerror != NULL)
  1106. *uerror = dlp->error_ack.dl_unix_errno;
  1107. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1108. dlp->error_ack.dl_unix_errno,
  1109. "recv_ack: %s: UNIX error", what);
  1110. if (dlp->error_ack.dl_unix_errno == EPERM ||
  1111. dlp->error_ack.dl_unix_errno == EACCES)
  1112. return (PCAP_ERROR_PERM_DENIED);
  1113. break;
  1114. default:
  1115. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1116. "recv_ack: %s: %s", what,
  1117. dlstrerror(errmsgbuf, sizeof (errmsgbuf), dlp->error_ack.dl_errno));
  1118. if (dlp->error_ack.dl_errno == DL_BADPPA)
  1119. return (PCAP_ERROR_NO_SUCH_DEVICE);
  1120. else if (dlp->error_ack.dl_errno == DL_ACCESS)
  1121. return (PCAP_ERROR_PERM_DENIED);
  1122. break;
  1123. }
  1124. return (PCAP_ERROR);
  1125. default:
  1126. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1127. "recv_ack: %s: Unexpected primitive ack %s",
  1128. what, dlprim(dlprimbuf, sizeof (dlprimbuf), dlp->dl_primitive));
  1129. return (PCAP_ERROR);
  1130. }
  1131. if (ctl.len < size) {
  1132. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1133. "recv_ack: %s: Ack too small (%d < %d)",
  1134. what, ctl.len, size);
  1135. return (PCAP_ERROR);
  1136. }
  1137. return (ctl.len);
  1138. }
  1139. static char *
  1140. dlstrerror(char *errbuf, size_t errbufsize, bpf_u_int32 dl_errno)
  1141. {
  1142. switch (dl_errno) {
  1143. case DL_ACCESS:
  1144. return ("Improper permissions for request");
  1145. case DL_BADADDR:
  1146. return ("DLSAP addr in improper format or invalid");
  1147. case DL_BADCORR:
  1148. return ("Seq number not from outstand DL_CONN_IND");
  1149. case DL_BADDATA:
  1150. return ("User data exceeded provider limit");
  1151. case DL_BADPPA:
  1152. #ifdef HAVE_DEV_DLPI
  1153. /*
  1154. * With a single "/dev/dlpi" device used for all
  1155. * DLPI providers, PPAs have nothing to do with
  1156. * unit numbers.
  1157. */
  1158. return ("Specified PPA was invalid");
  1159. #else
  1160. /*
  1161. * We have separate devices for separate devices;
  1162. * the PPA is just the unit number.
  1163. */
  1164. return ("Specified PPA (device unit) was invalid");
  1165. #endif
  1166. case DL_BADPRIM:
  1167. return ("Primitive received not known by provider");
  1168. case DL_BADQOSPARAM:
  1169. return ("QOS parameters contained invalid values");
  1170. case DL_BADQOSTYPE:
  1171. return ("QOS structure type is unknown/unsupported");
  1172. case DL_BADSAP:
  1173. return ("Bad LSAP selector");
  1174. case DL_BADTOKEN:
  1175. return ("Token used not an active stream");
  1176. case DL_BOUND:
  1177. return ("Attempted second bind with dl_max_conind");
  1178. case DL_INITFAILED:
  1179. return ("Physical link initialization failed");
  1180. case DL_NOADDR:
  1181. return ("Provider couldn't allocate alternate address");
  1182. case DL_NOTINIT:
  1183. return ("Physical link not initialized");
  1184. case DL_OUTSTATE:
  1185. return ("Primitive issued in improper state");
  1186. case DL_SYSERR:
  1187. return ("UNIX system error occurred");
  1188. case DL_UNSUPPORTED:
  1189. return ("Requested service not supplied by provider");
  1190. case DL_UNDELIVERABLE:
  1191. return ("Previous data unit could not be delivered");
  1192. case DL_NOTSUPPORTED:
  1193. return ("Primitive is known but not supported");
  1194. case DL_TOOMANY:
  1195. return ("Limit exceeded");
  1196. case DL_NOTENAB:
  1197. return ("Promiscuous mode not enabled");
  1198. case DL_BUSY:
  1199. return ("Other streams for PPA in post-attached");
  1200. case DL_NOAUTO:
  1201. return ("Automatic handling XID&TEST not supported");
  1202. case DL_NOXIDAUTO:
  1203. return ("Automatic handling of XID not supported");
  1204. case DL_NOTESTAUTO:
  1205. return ("Automatic handling of TEST not supported");
  1206. case DL_XIDAUTO:
  1207. return ("Automatic handling of XID response");
  1208. case DL_TESTAUTO:
  1209. return ("Automatic handling of TEST response");
  1210. case DL_PENDING:
  1211. return ("Pending outstanding connect indications");
  1212. default:
  1213. pcap_snprintf(errbuf, errbufsize, "Error %02x", dl_errno);
  1214. return (errbuf);
  1215. }
  1216. }
  1217. static char *
  1218. dlprim(char *primbuf, size_t primbufsize, bpf_u_int32 prim)
  1219. {
  1220. switch (prim) {
  1221. case DL_INFO_REQ:
  1222. return ("DL_INFO_REQ");
  1223. case DL_INFO_ACK:
  1224. return ("DL_INFO_ACK");
  1225. case DL_ATTACH_REQ:
  1226. return ("DL_ATTACH_REQ");
  1227. case DL_DETACH_REQ:
  1228. return ("DL_DETACH_REQ");
  1229. case DL_BIND_REQ:
  1230. return ("DL_BIND_REQ");
  1231. case DL_BIND_ACK:
  1232. return ("DL_BIND_ACK");
  1233. case DL_UNBIND_REQ:
  1234. return ("DL_UNBIND_REQ");
  1235. case DL_OK_ACK:
  1236. return ("DL_OK_ACK");
  1237. case DL_ERROR_ACK:
  1238. return ("DL_ERROR_ACK");
  1239. case DL_SUBS_BIND_REQ:
  1240. return ("DL_SUBS_BIND_REQ");
  1241. case DL_SUBS_BIND_ACK:
  1242. return ("DL_SUBS_BIND_ACK");
  1243. case DL_UNITDATA_REQ:
  1244. return ("DL_UNITDATA_REQ");
  1245. case DL_UNITDATA_IND:
  1246. return ("DL_UNITDATA_IND");
  1247. case DL_UDERROR_IND:
  1248. return ("DL_UDERROR_IND");
  1249. case DL_UDQOS_REQ:
  1250. return ("DL_UDQOS_REQ");
  1251. case DL_CONNECT_REQ:
  1252. return ("DL_CONNECT_REQ");
  1253. case DL_CONNECT_IND:
  1254. return ("DL_CONNECT_IND");
  1255. case DL_CONNECT_RES:
  1256. return ("DL_CONNECT_RES");
  1257. case DL_CONNECT_CON:
  1258. return ("DL_CONNECT_CON");
  1259. case DL_TOKEN_REQ:
  1260. return ("DL_TOKEN_REQ");
  1261. case DL_TOKEN_ACK:
  1262. return ("DL_TOKEN_ACK");
  1263. case DL_DISCONNECT_REQ:
  1264. return ("DL_DISCONNECT_REQ");
  1265. case DL_DISCONNECT_IND:
  1266. return ("DL_DISCONNECT_IND");
  1267. case DL_RESET_REQ:
  1268. return ("DL_RESET_REQ");
  1269. case DL_RESET_IND:
  1270. return ("DL_RESET_IND");
  1271. case DL_RESET_RES:
  1272. return ("DL_RESET_RES");
  1273. case DL_RESET_CON:
  1274. return ("DL_RESET_CON");
  1275. default:
  1276. pcap_snprintf(primbuf, primbufsize, "unknown primitive 0x%x",
  1277. prim);
  1278. return (primbuf);
  1279. }
  1280. }
  1281. static int
  1282. dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
  1283. {
  1284. dl_bind_req_t req;
  1285. memset((char *)&req, 0, sizeof(req));
  1286. req.dl_primitive = DL_BIND_REQ;
  1287. /* XXX - what if neither of these are defined? */
  1288. #if defined(DL_HP_RAWDLS)
  1289. req.dl_max_conind = 1; /* XXX magic number */
  1290. req.dl_service_mode = DL_HP_RAWDLS;
  1291. #elif defined(DL_CLDLS)
  1292. req.dl_service_mode = DL_CLDLS;
  1293. #endif
  1294. req.dl_sap = sap;
  1295. return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
  1296. }
  1297. static int
  1298. dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
  1299. {
  1300. return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
  1301. }
  1302. static int
  1303. dlokack(int fd, const char *what, char *bufp, char *ebuf)
  1304. {
  1305. return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL));
  1306. }
  1307. static int
  1308. dlinforeq(int fd, char *ebuf)
  1309. {
  1310. dl_info_req_t req;
  1311. req.dl_primitive = DL_INFO_REQ;
  1312. return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
  1313. }
  1314. static int
  1315. dlinfoack(int fd, char *bufp, char *ebuf)
  1316. {
  1317. return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
  1318. }
  1319. #ifdef HAVE_DL_PASSIVE_REQ_T
  1320. /*
  1321. * Enable DLPI passive mode. We do not care if this request fails, as this
  1322. * indicates the underlying DLPI device does not support link aggregation.
  1323. */
  1324. static void
  1325. dlpassive(int fd, char *ebuf)
  1326. {
  1327. dl_passive_req_t req;
  1328. bpf_u_int32 buf[MAXDLBUF];
  1329. req.dl_primitive = DL_PASSIVE_REQ;
  1330. if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
  1331. (void) dlokack(fd, "dlpassive", (char *)buf, ebuf);
  1332. }
  1333. #endif
  1334. #ifdef DL_HP_RAWDLS
  1335. /*
  1336. * There's an ack *if* there's an error.
  1337. */
  1338. static int
  1339. dlrawdatareq(int fd, const u_char *datap, int datalen)
  1340. {
  1341. struct strbuf ctl, data;
  1342. long buf[MAXDLBUF]; /* XXX - char? */
  1343. union DL_primitives *dlp;
  1344. int dlen;
  1345. dlp = MAKE_DL_PRIMITIVES(buf);
  1346. dlp->dl_primitive = DL_HP_RAWDATA_REQ;
  1347. dlen = DL_HP_RAWDATA_REQ_SIZE;
  1348. /*
  1349. * HP's documentation doesn't appear to show us supplying any
  1350. * address pointed to by the control part of the message.
  1351. * I think that's what raw mode means - you just send the raw
  1352. * packet, you don't specify where to send it to, as that's
  1353. * implied by the destination address.
  1354. */
  1355. ctl.maxlen = 0;
  1356. ctl.len = dlen;
  1357. ctl.buf = (void *)buf;
  1358. data.maxlen = 0;
  1359. data.len = datalen;
  1360. data.buf = (void *)datap;
  1361. return (putmsg(fd, &ctl, &data, 0));
  1362. }
  1363. #endif /* DL_HP_RAWDLS */
  1364. #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
  1365. static void
  1366. get_release(char *buf, size_t bufsize, bpf_u_int32 *majorp,
  1367. bpf_u_int32 *minorp, bpf_u_int32 *microp)
  1368. {
  1369. char *cp;
  1370. *majorp = 0;
  1371. *minorp = 0;
  1372. *microp = 0;
  1373. if (sysinfo(SI_RELEASE, buf, bufsize) < 0) {
  1374. strlcpy(buf, "?", bufsize);
  1375. return;
  1376. }
  1377. cp = buf;
  1378. if (!isdigit((unsigned char)*cp))
  1379. return;
  1380. *majorp = strtol(cp, &cp, 10);
  1381. if (*cp++ != '.')
  1382. return;
  1383. *minorp = strtol(cp, &cp, 10);
  1384. if (*cp++ != '.')
  1385. return;
  1386. *microp = strtol(cp, &cp, 10);
  1387. }
  1388. #endif
  1389. #ifdef DL_HP_PPA_REQ
  1390. /*
  1391. * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
  1392. */
  1393. /*
  1394. * Determine ppa number that specifies ifname.
  1395. *
  1396. * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
  1397. * the code that's used here is the old code for HP-UX 10.x.
  1398. *
  1399. * However, HP-UX 10.20, at least, appears to have such a member
  1400. * in its "dl_hp_ppa_info_t" structure, so the new code is used.
  1401. * The new code didn't work on an old 10.20 system on which Rick
  1402. * Jones of HP tried it, but with later patches installed, it
  1403. * worked - it appears that the older system had those members but
  1404. * didn't put anything in them, so, if the search by name fails, we
  1405. * do the old search.
  1406. *
  1407. * Rick suggests that making sure your system is "up on the latest
  1408. * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
  1409. * that problem, as well as allowing libpcap to see packets sent
  1410. * from the system on which the libpcap application is being run.
  1411. * (On 10.20, in addition to getting the latest patches, you need
  1412. * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
  1413. * a posting to "comp.sys.hp.hpux" at
  1414. *
  1415. * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
  1416. *
  1417. * says that, to see the machine's outgoing traffic, you'd need to
  1418. * apply the right patches to your system, and also set that variable
  1419. * with:
  1420. echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
  1421. * which could be put in, for example, "/sbin/init.d/lan".
  1422. *
  1423. * Setting the variable is not necessary on HP-UX 11.x.
  1424. */
  1425. static int
  1426. get_dlpi_ppa(register int fd, register const char *device, register u_int unit,
  1427. u_int *ppa, register char *ebuf)
  1428. {
  1429. register dl_hp_ppa_ack_t *ap;
  1430. register dl_hp_ppa_info_t *ipstart, *ip;
  1431. register u_int i;
  1432. char dname[100];
  1433. register u_long majdev;
  1434. struct stat statbuf;
  1435. dl_hp_ppa_req_t req;
  1436. char buf[MAXDLBUF];
  1437. char *ppa_data_buf;
  1438. dl_hp_ppa_ack_t *dlp;
  1439. struct strbuf ctl;
  1440. int flags;
  1441. memset((char *)&req, 0, sizeof(req));
  1442. req.dl_primitive = DL_HP_PPA_REQ;
  1443. memset((char *)buf, 0, sizeof(buf));
  1444. if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
  1445. return (PCAP_ERROR);
  1446. ctl.maxlen = DL_HP_PPA_ACK_SIZE;
  1447. ctl.len = 0;
  1448. ctl.buf = (char *)buf;
  1449. flags = 0;
  1450. /*
  1451. * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
  1452. * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
  1453. * which is NOT big enough for a DL_HP_PPA_REQ.
  1454. *
  1455. * This causes libpcap applications to fail on a system with HP-APA
  1456. * installed.
  1457. *
  1458. * To figure out how big the returned data is, we first call getmsg
  1459. * to get the small head and peek at the head to get the actual data
  1460. * length, and then issue another getmsg to get the actual PPA data.
  1461. */
  1462. /* get the head first */
  1463. if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
  1464. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1465. errno, "get_dlpi_ppa: hpppa getmsg");
  1466. return (PCAP_ERROR);
  1467. }
  1468. if (ctl.len == -1) {
  1469. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1470. "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
  1471. return (PCAP_ERROR);
  1472. }
  1473. dlp = (dl_hp_ppa_ack_t *)ctl.buf;
  1474. if (dlp->dl_primitive != DL_HP_PPA_ACK) {
  1475. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1476. "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
  1477. (bpf_u_int32)dlp->dl_primitive);
  1478. return (PCAP_ERROR);
  1479. }
  1480. if ((size_t)ctl.len < DL_HP_PPA_ACK_SIZE) {
  1481. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1482. "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
  1483. ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
  1484. return (PCAP_ERROR);
  1485. }
  1486. /* allocate buffer */
  1487. if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
  1488. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1489. errno, "get_dlpi_ppa: hpppa malloc");
  1490. return (PCAP_ERROR);
  1491. }
  1492. ctl.maxlen = dlp->dl_length;
  1493. ctl.len = 0;
  1494. ctl.buf = (char *)ppa_data_buf;
  1495. /* get the data */
  1496. if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
  1497. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1498. errno, "get_dlpi_ppa: hpppa getmsg");
  1499. free(ppa_data_buf);
  1500. return (PCAP_ERROR);
  1501. }
  1502. if (ctl.len == -1) {
  1503. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1504. "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
  1505. return (PCAP_ERROR);
  1506. }
  1507. if ((u_int)ctl.len < dlp->dl_length) {
  1508. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1509. "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
  1510. ctl.len, (unsigned long)dlp->dl_length);
  1511. free(ppa_data_buf);
  1512. return (PCAP_ERROR);
  1513. }
  1514. ap = (dl_hp_ppa_ack_t *)buf;
  1515. ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
  1516. ip = ipstart;
  1517. #ifdef HAVE_DL_HP_PPA_INFO_T_DL_MODULE_ID_1
  1518. /*
  1519. * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
  1520. * member that should, in theory, contain the part of the
  1521. * name for the device that comes before the unit number,
  1522. * and should also have a "dl_module_id_2" member that may
  1523. * contain an alternate name (e.g., I think Ethernet devices
  1524. * have both "lan", for "lanN", and "snap", for "snapN", with
  1525. * the former being for Ethernet packets and the latter being
  1526. * for 802.3/802.2 packets).
  1527. *
  1528. * Search for the device that has the specified name and
  1529. * instance number.
  1530. */
  1531. for (i = 0; i < ap->dl_count; i++) {
  1532. if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
  1533. strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
  1534. ip->dl_instance_num == unit)
  1535. break;
  1536. ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
  1537. }
  1538. #else
  1539. /*
  1540. * We don't have that member, so the search is impossible; make it
  1541. * look as if the search failed.
  1542. */
  1543. i = ap->dl_count;
  1544. #endif
  1545. if (i == ap->dl_count) {
  1546. /*
  1547. * Well, we didn't, or can't, find the device by name.
  1548. *
  1549. * HP-UX 10.20, whilst it has "dl_module_id_1" and
  1550. * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
  1551. * doesn't seem to fill them in unless the system is
  1552. * at a reasonably up-to-date patch level.
  1553. *
  1554. * Older HP-UX 10.x systems might not have those fields
  1555. * at all.
  1556. *
  1557. * Therefore, we'll search for the entry with the major
  1558. * device number of a device with the name "/dev/<dev><unit>",
  1559. * if such a device exists, as the old code did.
  1560. */
  1561. pcap_snprintf(dname, sizeof(dname), "/dev/%s%u", device, unit);
  1562. if (stat(dname, &statbuf) < 0) {
  1563. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1564. errno, "stat: %s", dname);
  1565. return (PCAP_ERROR);
  1566. }
  1567. majdev = major(statbuf.st_rdev);
  1568. ip = ipstart;
  1569. for (i = 0; i < ap->dl_count; i++) {
  1570. if (ip->dl_mjr_num == majdev &&
  1571. ip->dl_instance_num == unit)
  1572. break;
  1573. ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
  1574. }
  1575. }
  1576. if (i == ap->dl_count) {
  1577. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1578. "can't find /dev/dlpi PPA for %s%u", device, unit);
  1579. return (PCAP_ERROR_NO_SUCH_DEVICE);
  1580. }
  1581. if (ip->dl_hdw_state == HDW_DEAD) {
  1582. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1583. "%s%d: hardware state: DOWN\n", device, unit);
  1584. free(ppa_data_buf);
  1585. return (PCAP_ERROR);
  1586. }
  1587. *ppa = ip->dl_ppa;
  1588. free(ppa_data_buf);
  1589. return (0);
  1590. }
  1591. #endif
  1592. #ifdef HAVE_HPUX9
  1593. /*
  1594. * Under HP-UX 9, there is no good way to determine the ppa.
  1595. * So punt and read it from /dev/kmem.
  1596. */
  1597. static struct nlist nl[] = {
  1598. #define NL_IFNET 0
  1599. { "ifnet" },
  1600. { "" }
  1601. };
  1602. static char path_vmunix[] = "/hp-ux";
  1603. /* Determine ppa number that specifies ifname */
  1604. static int
  1605. get_dlpi_ppa(register int fd, register const char *ifname, register u_int unit,
  1606. u_int *ppa, register char *ebuf)
  1607. {
  1608. register const char *cp;
  1609. register int kd;
  1610. void *addr;
  1611. struct ifnet ifnet;
  1612. char if_name[sizeof(ifnet.if_name) + 1];
  1613. cp = strrchr(ifname, '/');
  1614. if (cp != NULL)
  1615. ifname = cp + 1;
  1616. if (nlist(path_vmunix, &nl) < 0) {
  1617. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
  1618. path_vmunix);
  1619. return (PCAP_ERROR);
  1620. }
  1621. if (nl[NL_IFNET].n_value == 0) {
  1622. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
  1623. "could't find %s kernel symbol",
  1624. nl[NL_IFNET].n_name);
  1625. return (PCAP_ERROR);
  1626. }
  1627. kd = open("/dev/kmem", O_RDONLY);
  1628. if (kd < 0) {
  1629. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1630. errno, "kmem open");
  1631. return (PCAP_ERROR);
  1632. }
  1633. if (dlpi_kread(kd, nl[NL_IFNET].n_value,
  1634. &addr, sizeof(addr), ebuf) < 0) {
  1635. close(kd);
  1636. return (PCAP_ERROR);
  1637. }
  1638. for (; addr != NULL; addr = ifnet.if_next) {
  1639. if (dlpi_kread(kd, (off_t)addr,
  1640. &ifnet, sizeof(ifnet), ebuf) < 0 ||
  1641. dlpi_kread(kd, (off_t)ifnet.if_name,
  1642. if_name, sizeof(ifnet.if_name), ebuf) < 0) {
  1643. (void)close(kd);
  1644. return (PCAP_ERROR);
  1645. }
  1646. if_name[sizeof(ifnet.if_name)] = '\0';
  1647. if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit) {
  1648. *ppa = ifnet.if_index;
  1649. return (0);
  1650. }
  1651. }
  1652. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
  1653. return (PCAP_ERROR_NO_SUCH_DEVICE);
  1654. }
  1655. static int
  1656. dlpi_kread(register int fd, register off_t addr,
  1657. register void *buf, register u_int len, register char *ebuf)
  1658. {
  1659. register int cc;
  1660. if (lseek(fd, addr, SEEK_SET) < 0) {
  1661. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1662. errno, "lseek");
  1663. return (-1);
  1664. }
  1665. cc = read(fd, buf, len);
  1666. if (cc < 0) {
  1667. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  1668. errno, "read");
  1669. return (-1);
  1670. } else if (cc != len) {
  1671. pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
  1672. len);
  1673. return (-1);
  1674. }
  1675. return (cc);
  1676. }
  1677. #endif
  1678. pcap_t *
  1679. pcap_create_interface(const char *device _U_, char *ebuf)
  1680. {
  1681. pcap_t *p;
  1682. #ifdef DL_HP_RAWDLS
  1683. struct pcap_dlpi *pd;
  1684. #endif
  1685. p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
  1686. if (p == NULL)
  1687. return (NULL);
  1688. #ifdef DL_HP_RAWDLS
  1689. pd = p->priv;
  1690. pd->send_fd = -1; /* it hasn't been opened yet */
  1691. #endif
  1692. p->activate_op = pcap_activate_dlpi;
  1693. return (p);
  1694. }
  1695. /*
  1696. * Libpcap version string.
  1697. */
  1698. const char *
  1699. pcap_lib_version(void)
  1700. {
  1701. return (PCAP_VERSION_STRING);
  1702. }