print-fr.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
  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. /* \summary: Frame Relay printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "netdissect.h"
  29. #include "addrtoname.h"
  30. #include "ethertype.h"
  31. #include "llc.h"
  32. #include "nlpid.h"
  33. #include "extract.h"
  34. #include "oui.h"
  35. static void frf15_print(netdissect_options *ndo, const u_char *, u_int);
  36. /*
  37. * the frame relay header has a variable length
  38. *
  39. * the EA bit determines if there is another byte
  40. * in the header
  41. *
  42. * minimum header length is 2 bytes
  43. * maximum header length is 4 bytes
  44. *
  45. * 7 6 5 4 3 2 1 0
  46. * +----+----+----+----+----+----+----+----+
  47. * | DLCI (6 bits) | CR | EA |
  48. * +----+----+----+----+----+----+----+----+
  49. * | DLCI (4 bits) |FECN|BECN| DE | EA |
  50. * +----+----+----+----+----+----+----+----+
  51. * | DLCI (7 bits) | EA |
  52. * +----+----+----+----+----+----+----+----+
  53. * | DLCI (6 bits) |SDLC| EA |
  54. * +----+----+----+----+----+----+----+----+
  55. */
  56. #define FR_EA_BIT 0x01
  57. #define FR_CR_BIT 0x02000000
  58. #define FR_DE_BIT 0x00020000
  59. #define FR_BECN_BIT 0x00040000
  60. #define FR_FECN_BIT 0x00080000
  61. #define FR_SDLC_BIT 0x00000002
  62. static const struct tok fr_header_flag_values[] = {
  63. { FR_CR_BIT, "C!" },
  64. { FR_DE_BIT, "DE" },
  65. { FR_BECN_BIT, "BECN" },
  66. { FR_FECN_BIT, "FECN" },
  67. { FR_SDLC_BIT, "sdlcore" },
  68. { 0, NULL }
  69. };
  70. /* FRF.15 / FRF.16 */
  71. #define MFR_B_BIT 0x80
  72. #define MFR_E_BIT 0x40
  73. #define MFR_C_BIT 0x20
  74. #define MFR_BEC_MASK (MFR_B_BIT | MFR_E_BIT | MFR_C_BIT)
  75. #define MFR_CTRL_FRAME (MFR_B_BIT | MFR_E_BIT | MFR_C_BIT)
  76. #define MFR_FRAG_FRAME (MFR_B_BIT | MFR_E_BIT )
  77. static const struct tok frf_flag_values[] = {
  78. { MFR_B_BIT, "Begin" },
  79. { MFR_E_BIT, "End" },
  80. { MFR_C_BIT, "Control" },
  81. { 0, NULL }
  82. };
  83. /* Finds out Q.922 address length, DLCI and flags. Returns 1 on success,
  84. * 0 on invalid address, -1 on truncated packet
  85. * save the flags dep. on address length
  86. */
  87. static int parse_q922_addr(netdissect_options *ndo,
  88. const u_char *p, u_int *dlci,
  89. u_int *addr_len, uint8_t *flags, u_int length)
  90. {
  91. if (!ND_TTEST(p[0]) || length < 1)
  92. return -1;
  93. if ((p[0] & FR_EA_BIT))
  94. return 0;
  95. if (!ND_TTEST(p[1]) || length < 2)
  96. return -1;
  97. *addr_len = 2;
  98. *dlci = ((p[0] & 0xFC) << 2) | ((p[1] & 0xF0) >> 4);
  99. flags[0] = p[0] & 0x02; /* populate the first flag fields */
  100. flags[1] = p[1] & 0x0c;
  101. flags[2] = 0; /* clear the rest of the flags */
  102. flags[3] = 0;
  103. if (p[1] & FR_EA_BIT)
  104. return 1; /* 2-byte Q.922 address */
  105. p += 2;
  106. length -= 2;
  107. if (!ND_TTEST(p[0]) || length < 1)
  108. return -1;
  109. (*addr_len)++; /* 3- or 4-byte Q.922 address */
  110. if ((p[0] & FR_EA_BIT) == 0) {
  111. *dlci = (*dlci << 7) | (p[0] >> 1);
  112. (*addr_len)++; /* 4-byte Q.922 address */
  113. p++;
  114. length--;
  115. }
  116. if (!ND_TTEST(p[0]) || length < 1)
  117. return -1;
  118. if ((p[0] & FR_EA_BIT) == 0)
  119. return 0; /* more than 4 bytes of Q.922 address? */
  120. flags[3] = p[0] & 0x02;
  121. *dlci = (*dlci << 6) | (p[0] >> 2);
  122. return 1;
  123. }
  124. char *
  125. q922_string(netdissect_options *ndo, const u_char *p, u_int length)
  126. {
  127. static u_int dlci, addr_len;
  128. static uint8_t flags[4];
  129. static char buffer[sizeof("DLCI xxxxxxxxxx")];
  130. memset(buffer, 0, sizeof(buffer));
  131. if (parse_q922_addr(ndo, p, &dlci, &addr_len, flags, length) == 1){
  132. snprintf(buffer, sizeof(buffer), "DLCI %u", dlci);
  133. }
  134. return buffer;
  135. }
  136. /* Frame Relay packet structure, with flags and CRC removed
  137. +---------------------------+
  138. | Q.922 Address* |
  139. +-- --+
  140. | |
  141. +---------------------------+
  142. | Control (UI = 0x03) |
  143. +---------------------------+
  144. | Optional Pad (0x00) |
  145. +---------------------------+
  146. | NLPID |
  147. +---------------------------+
  148. | . |
  149. | . |
  150. | . |
  151. | Data |
  152. | . |
  153. | . |
  154. +---------------------------+
  155. * Q.922 addresses, as presently defined, are two octets and
  156. contain a 10-bit DLCI. In some networks Q.922 addresses
  157. may optionally be increased to three or four octets.
  158. */
  159. static void
  160. fr_hdr_print(netdissect_options *ndo,
  161. int length, u_int addr_len, u_int dlci, uint8_t *flags, uint16_t nlpid)
  162. {
  163. if (ndo->ndo_qflag) {
  164. ND_PRINT((ndo, "Q.922, DLCI %u, length %u: ",
  165. dlci,
  166. length));
  167. } else {
  168. if (nlpid <= 0xff) /* if its smaller than 256 then its a NLPID */
  169. ND_PRINT((ndo, "Q.922, hdr-len %u, DLCI %u, Flags [%s], NLPID %s (0x%02x), length %u: ",
  170. addr_len,
  171. dlci,
  172. bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
  173. tok2str(nlpid_values,"unknown", nlpid),
  174. nlpid,
  175. length));
  176. else /* must be an ethertype */
  177. ND_PRINT((ndo, "Q.922, hdr-len %u, DLCI %u, Flags [%s], cisco-ethertype %s (0x%04x), length %u: ",
  178. addr_len,
  179. dlci,
  180. bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
  181. tok2str(ethertype_values, "unknown", nlpid),
  182. nlpid,
  183. length));
  184. }
  185. }
  186. u_int
  187. fr_if_print(netdissect_options *ndo,
  188. const struct pcap_pkthdr *h, register const u_char *p)
  189. {
  190. register u_int length = h->len;
  191. register u_int caplen = h->caplen;
  192. ND_TCHECK2(*p, 4); /* minimum frame header length */
  193. if ((length = fr_print(ndo, p, length)) == 0)
  194. return (0);
  195. else
  196. return length;
  197. trunc:
  198. ND_PRINT((ndo, "[|fr]"));
  199. return caplen;
  200. }
  201. u_int
  202. fr_print(netdissect_options *ndo,
  203. register const u_char *p, u_int length)
  204. {
  205. int ret;
  206. uint16_t extracted_ethertype;
  207. u_int dlci;
  208. u_int addr_len;
  209. uint16_t nlpid;
  210. u_int hdr_len;
  211. uint8_t flags[4];
  212. ret = parse_q922_addr(ndo, p, &dlci, &addr_len, flags, length);
  213. if (ret == -1)
  214. goto trunc;
  215. if (ret == 0) {
  216. ND_PRINT((ndo, "Q.922, invalid address"));
  217. return 0;
  218. }
  219. ND_TCHECK(p[addr_len]);
  220. if (length < addr_len + 1)
  221. goto trunc;
  222. if (p[addr_len] != LLC_UI && dlci != 0) {
  223. /*
  224. * Let's figure out if we have Cisco-style encapsulation,
  225. * with an Ethernet type (Cisco HDLC type?) following the
  226. * address.
  227. */
  228. if (!ND_TTEST2(p[addr_len], 2) || length < addr_len + 2) {
  229. /* no Ethertype */
  230. ND_PRINT((ndo, "UI %02x! ", p[addr_len]));
  231. } else {
  232. extracted_ethertype = EXTRACT_16BITS(p+addr_len);
  233. if (ndo->ndo_eflag)
  234. fr_hdr_print(ndo, length, addr_len, dlci,
  235. flags, extracted_ethertype);
  236. if (ethertype_print(ndo, extracted_ethertype,
  237. p+addr_len+ETHERTYPE_LEN,
  238. length-addr_len-ETHERTYPE_LEN,
  239. ndo->ndo_snapend-p-addr_len-ETHERTYPE_LEN,
  240. NULL, NULL) == 0)
  241. /* ether_type not known, probably it wasn't one */
  242. ND_PRINT((ndo, "UI %02x! ", p[addr_len]));
  243. else
  244. return addr_len + 2;
  245. }
  246. }
  247. ND_TCHECK(p[addr_len+1]);
  248. if (length < addr_len + 2)
  249. goto trunc;
  250. if (p[addr_len + 1] == 0) {
  251. /*
  252. * Assume a pad byte after the control (UI) byte.
  253. * A pad byte should only be used with 3-byte Q.922.
  254. */
  255. if (addr_len != 3)
  256. ND_PRINT((ndo, "Pad! "));
  257. hdr_len = addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
  258. } else {
  259. /*
  260. * Not a pad byte.
  261. * A pad byte should be used with 3-byte Q.922.
  262. */
  263. if (addr_len == 3)
  264. ND_PRINT((ndo, "No pad! "));
  265. hdr_len = addr_len + 1 /* UI */ + 1 /* NLPID */;
  266. }
  267. ND_TCHECK(p[hdr_len - 1]);
  268. if (length < hdr_len)
  269. goto trunc;
  270. nlpid = p[hdr_len - 1];
  271. if (ndo->ndo_eflag)
  272. fr_hdr_print(ndo, length, addr_len, dlci, flags, nlpid);
  273. p += hdr_len;
  274. length -= hdr_len;
  275. switch (nlpid) {
  276. case NLPID_IP:
  277. ip_print(ndo, p, length);
  278. break;
  279. case NLPID_IP6:
  280. ip6_print(ndo, p, length);
  281. break;
  282. case NLPID_CLNP:
  283. case NLPID_ESIS:
  284. case NLPID_ISIS:
  285. isoclns_print(ndo, p - 1, length + 1); /* OSI printers need the NLPID field */
  286. break;
  287. case NLPID_SNAP:
  288. if (snap_print(ndo, p, length, ndo->ndo_snapend - p, NULL, NULL, 0) == 0) {
  289. /* ether_type not known, print raw packet */
  290. if (!ndo->ndo_eflag)
  291. fr_hdr_print(ndo, length + hdr_len, hdr_len,
  292. dlci, flags, nlpid);
  293. if (!ndo->ndo_suppress_default_print)
  294. ND_DEFAULTPRINT(p - hdr_len, length + hdr_len);
  295. }
  296. break;
  297. case NLPID_Q933:
  298. q933_print(ndo, p, length);
  299. break;
  300. case NLPID_MFR:
  301. frf15_print(ndo, p, length);
  302. break;
  303. case NLPID_PPP:
  304. ppp_print(ndo, p, length);
  305. break;
  306. default:
  307. if (!ndo->ndo_eflag)
  308. fr_hdr_print(ndo, length + hdr_len, addr_len,
  309. dlci, flags, nlpid);
  310. if (!ndo->ndo_xflag)
  311. ND_DEFAULTPRINT(p, length);
  312. }
  313. return hdr_len;
  314. trunc:
  315. ND_PRINT((ndo, "[|fr]"));
  316. return 0;
  317. }
  318. u_int
  319. mfr_if_print(netdissect_options *ndo,
  320. const struct pcap_pkthdr *h, register const u_char *p)
  321. {
  322. register u_int length = h->len;
  323. register u_int caplen = h->caplen;
  324. ND_TCHECK2(*p, 2); /* minimum frame header length */
  325. if ((length = mfr_print(ndo, p, length)) == 0)
  326. return (0);
  327. else
  328. return length;
  329. trunc:
  330. ND_PRINT((ndo, "[|mfr]"));
  331. return caplen;
  332. }
  333. #define MFR_CTRL_MSG_ADD_LINK 1
  334. #define MFR_CTRL_MSG_ADD_LINK_ACK 2
  335. #define MFR_CTRL_MSG_ADD_LINK_REJ 3
  336. #define MFR_CTRL_MSG_HELLO 4
  337. #define MFR_CTRL_MSG_HELLO_ACK 5
  338. #define MFR_CTRL_MSG_REMOVE_LINK 6
  339. #define MFR_CTRL_MSG_REMOVE_LINK_ACK 7
  340. static const struct tok mfr_ctrl_msg_values[] = {
  341. { MFR_CTRL_MSG_ADD_LINK, "Add Link" },
  342. { MFR_CTRL_MSG_ADD_LINK_ACK, "Add Link ACK" },
  343. { MFR_CTRL_MSG_ADD_LINK_REJ, "Add Link Reject" },
  344. { MFR_CTRL_MSG_HELLO, "Hello" },
  345. { MFR_CTRL_MSG_HELLO_ACK, "Hello ACK" },
  346. { MFR_CTRL_MSG_REMOVE_LINK, "Remove Link" },
  347. { MFR_CTRL_MSG_REMOVE_LINK_ACK, "Remove Link ACK" },
  348. { 0, NULL }
  349. };
  350. #define MFR_CTRL_IE_BUNDLE_ID 1
  351. #define MFR_CTRL_IE_LINK_ID 2
  352. #define MFR_CTRL_IE_MAGIC_NUM 3
  353. #define MFR_CTRL_IE_TIMESTAMP 5
  354. #define MFR_CTRL_IE_VENDOR_EXT 6
  355. #define MFR_CTRL_IE_CAUSE 7
  356. static const struct tok mfr_ctrl_ie_values[] = {
  357. { MFR_CTRL_IE_BUNDLE_ID, "Bundle ID"},
  358. { MFR_CTRL_IE_LINK_ID, "Link ID"},
  359. { MFR_CTRL_IE_MAGIC_NUM, "Magic Number"},
  360. { MFR_CTRL_IE_TIMESTAMP, "Timestamp"},
  361. { MFR_CTRL_IE_VENDOR_EXT, "Vendor Extension"},
  362. { MFR_CTRL_IE_CAUSE, "Cause"},
  363. { 0, NULL }
  364. };
  365. #define MFR_ID_STRING_MAXLEN 50
  366. struct ie_tlv_header_t {
  367. uint8_t ie_type;
  368. uint8_t ie_len;
  369. };
  370. u_int
  371. mfr_print(netdissect_options *ndo,
  372. register const u_char *p, u_int length)
  373. {
  374. u_int tlen,idx,hdr_len = 0;
  375. uint16_t sequence_num;
  376. uint8_t ie_type,ie_len;
  377. const uint8_t *tptr;
  378. /*
  379. * FRF.16 Link Integrity Control Frame
  380. *
  381. * 7 6 5 4 3 2 1 0
  382. * +----+----+----+----+----+----+----+----+
  383. * | B | E | C=1| 0 0 0 0 | EA |
  384. * +----+----+----+----+----+----+----+----+
  385. * | 0 0 0 0 0 0 0 0 |
  386. * +----+----+----+----+----+----+----+----+
  387. * | message type |
  388. * +----+----+----+----+----+----+----+----+
  389. */
  390. ND_TCHECK2(*p, 4); /* minimum frame header length */
  391. if ((p[0] & MFR_BEC_MASK) == MFR_CTRL_FRAME && p[1] == 0) {
  392. ND_PRINT((ndo, "FRF.16 Control, Flags [%s], %s, length %u",
  393. bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK)),
  394. tok2str(mfr_ctrl_msg_values,"Unknown Message (0x%02x)",p[2]),
  395. length));
  396. tptr = p + 3;
  397. tlen = length -3;
  398. hdr_len = 3;
  399. if (!ndo->ndo_vflag)
  400. return hdr_len;
  401. while (tlen>sizeof(struct ie_tlv_header_t)) {
  402. ND_TCHECK2(*tptr, sizeof(struct ie_tlv_header_t));
  403. ie_type=tptr[0];
  404. ie_len=tptr[1];
  405. ND_PRINT((ndo, "\n\tIE %s (%u), length %u: ",
  406. tok2str(mfr_ctrl_ie_values,"Unknown",ie_type),
  407. ie_type,
  408. ie_len));
  409. /* infinite loop check */
  410. if (ie_type == 0 || ie_len <= sizeof(struct ie_tlv_header_t))
  411. return hdr_len;
  412. ND_TCHECK2(*tptr, ie_len);
  413. tptr+=sizeof(struct ie_tlv_header_t);
  414. /* tlv len includes header */
  415. ie_len-=sizeof(struct ie_tlv_header_t);
  416. tlen-=sizeof(struct ie_tlv_header_t);
  417. switch (ie_type) {
  418. case MFR_CTRL_IE_MAGIC_NUM:
  419. ND_PRINT((ndo, "0x%08x", EXTRACT_32BITS(tptr)));
  420. break;
  421. case MFR_CTRL_IE_BUNDLE_ID: /* same message format */
  422. case MFR_CTRL_IE_LINK_ID:
  423. for (idx = 0; idx < ie_len && idx < MFR_ID_STRING_MAXLEN; idx++) {
  424. if (*(tptr+idx) != 0) /* don't print null termination */
  425. safeputchar(ndo, *(tptr + idx));
  426. else
  427. break;
  428. }
  429. break;
  430. case MFR_CTRL_IE_TIMESTAMP:
  431. if (ie_len == sizeof(struct timeval)) {
  432. ts_print(ndo, (const struct timeval *)tptr);
  433. break;
  434. }
  435. /* fall through and hexdump if no unix timestamp */
  436. /*
  437. * FIXME those are the defined IEs that lack a decoder
  438. * you are welcome to contribute code ;-)
  439. */
  440. case MFR_CTRL_IE_VENDOR_EXT:
  441. case MFR_CTRL_IE_CAUSE:
  442. default:
  443. if (ndo->ndo_vflag <= 1)
  444. print_unknown_data(ndo, tptr, "\n\t ", ie_len);
  445. break;
  446. }
  447. /* do we want to see a hexdump of the IE ? */
  448. if (ndo->ndo_vflag > 1 )
  449. print_unknown_data(ndo, tptr, "\n\t ", ie_len);
  450. tlen-=ie_len;
  451. tptr+=ie_len;
  452. }
  453. return hdr_len;
  454. }
  455. /*
  456. * FRF.16 Fragmentation Frame
  457. *
  458. * 7 6 5 4 3 2 1 0
  459. * +----+----+----+----+----+----+----+----+
  460. * | B | E | C=0|seq. (high 4 bits) | EA |
  461. * +----+----+----+----+----+----+----+----+
  462. * | sequence (low 8 bits) |
  463. * +----+----+----+----+----+----+----+----+
  464. * | DLCI (6 bits) | CR | EA |
  465. * +----+----+----+----+----+----+----+----+
  466. * | DLCI (4 bits) |FECN|BECN| DE | EA |
  467. * +----+----+----+----+----+----+----+----+
  468. */
  469. sequence_num = (p[0]&0x1e)<<7 | p[1];
  470. /* whole packet or first fragment ? */
  471. if ((p[0] & MFR_BEC_MASK) == MFR_FRAG_FRAME ||
  472. (p[0] & MFR_BEC_MASK) == MFR_B_BIT) {
  473. ND_PRINT((ndo, "FRF.16 Frag, seq %u, Flags [%s], ",
  474. sequence_num,
  475. bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK))));
  476. hdr_len = 2;
  477. fr_print(ndo, p+hdr_len,length-hdr_len);
  478. return hdr_len;
  479. }
  480. /* must be a middle or the last fragment */
  481. ND_PRINT((ndo, "FRF.16 Frag, seq %u, Flags [%s]",
  482. sequence_num,
  483. bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK))));
  484. print_unknown_data(ndo, p, "\n\t", length);
  485. return hdr_len;
  486. trunc:
  487. ND_PRINT((ndo, "[|mfr]"));
  488. return length;
  489. }
  490. /* an NLPID of 0xb1 indicates a 2-byte
  491. * FRF.15 header
  492. *
  493. * 7 6 5 4 3 2 1 0
  494. * +----+----+----+----+----+----+----+----+
  495. * ~ Q.922 header ~
  496. * +----+----+----+----+----+----+----+----+
  497. * | NLPID (8 bits) | NLPID=0xb1
  498. * +----+----+----+----+----+----+----+----+
  499. * | B | E | C |seq. (high 4 bits) | R |
  500. * +----+----+----+----+----+----+----+----+
  501. * | sequence (low 8 bits) |
  502. * +----+----+----+----+----+----+----+----+
  503. */
  504. #define FR_FRF15_FRAGTYPE 0x01
  505. static void
  506. frf15_print(netdissect_options *ndo,
  507. const u_char *p, u_int length)
  508. {
  509. uint16_t sequence_num, flags;
  510. if (length < 2)
  511. goto trunc;
  512. ND_TCHECK2(*p, 2);
  513. flags = p[0]&MFR_BEC_MASK;
  514. sequence_num = (p[0]&0x1e)<<7 | p[1];
  515. ND_PRINT((ndo, "FRF.15, seq 0x%03x, Flags [%s],%s Fragmentation, length %u",
  516. sequence_num,
  517. bittok2str(frf_flag_values,"none",flags),
  518. p[0]&FR_FRF15_FRAGTYPE ? "Interface" : "End-to-End",
  519. length));
  520. /* TODO:
  521. * depending on all permutations of the B, E and C bit
  522. * dig as deep as we can - e.g. on the first (B) fragment
  523. * there is enough payload to print the IP header
  524. * on non (B) fragments it depends if the fragmentation
  525. * model is end-to-end or interface based wether we want to print
  526. * another Q.922 header
  527. */
  528. return;
  529. trunc:
  530. ND_PRINT((ndo, "[|frf.15]"));
  531. }
  532. /*
  533. * Q.933 decoding portion for framerelay specific.
  534. */
  535. /* Q.933 packet format
  536. Format of Other Protocols
  537. using Q.933 NLPID
  538. +-------------------------------+
  539. | Q.922 Address |
  540. +---------------+---------------+
  541. |Control 0x03 | NLPID 0x08 |
  542. +---------------+---------------+
  543. | L2 Protocol ID |
  544. | octet 1 | octet 2 |
  545. +-------------------------------+
  546. | L3 Protocol ID |
  547. | octet 2 | octet 2 |
  548. +-------------------------------+
  549. | Protocol Data |
  550. +-------------------------------+
  551. | FCS |
  552. +-------------------------------+
  553. */
  554. /* L2 (Octet 1)- Call Reference Usually is 0x0 */
  555. /*
  556. * L2 (Octet 2)- Message Types definition 1 byte long.
  557. */
  558. /* Call Establish */
  559. #define MSG_TYPE_ESC_TO_NATIONAL 0x00
  560. #define MSG_TYPE_ALERT 0x01
  561. #define MSG_TYPE_CALL_PROCEEDING 0x02
  562. #define MSG_TYPE_CONNECT 0x07
  563. #define MSG_TYPE_CONNECT_ACK 0x0F
  564. #define MSG_TYPE_PROGRESS 0x03
  565. #define MSG_TYPE_SETUP 0x05
  566. /* Call Clear */
  567. #define MSG_TYPE_DISCONNECT 0x45
  568. #define MSG_TYPE_RELEASE 0x4D
  569. #define MSG_TYPE_RELEASE_COMPLETE 0x5A
  570. #define MSG_TYPE_RESTART 0x46
  571. #define MSG_TYPE_RESTART_ACK 0x4E
  572. /* Status */
  573. #define MSG_TYPE_STATUS 0x7D
  574. #define MSG_TYPE_STATUS_ENQ 0x75
  575. static const struct tok fr_q933_msg_values[] = {
  576. { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
  577. { MSG_TYPE_ALERT, "Alert" },
  578. { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
  579. { MSG_TYPE_CONNECT, "Connect" },
  580. { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
  581. { MSG_TYPE_PROGRESS, "Progress" },
  582. { MSG_TYPE_SETUP, "Setup" },
  583. { MSG_TYPE_DISCONNECT, "Disconnect" },
  584. { MSG_TYPE_RELEASE, "Release" },
  585. { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
  586. { MSG_TYPE_RESTART, "Restart" },
  587. { MSG_TYPE_RESTART_ACK, "Restart ACK" },
  588. { MSG_TYPE_STATUS, "Status Reply" },
  589. { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
  590. { 0, NULL }
  591. };
  592. #define IE_IS_SINGLE_OCTET(iecode) ((iecode) & 0x80)
  593. #define IE_IS_SHIFT(iecode) (((iecode) & 0xF0) == 0x90)
  594. #define IE_SHIFT_IS_NON_LOCKING(iecode) ((iecode) & 0x08)
  595. #define IE_SHIFT_IS_LOCKING(iecode) (!(IE_SHIFT_IS_NON_LOCKING(iecode)))
  596. #define IE_SHIFT_CODESET(iecode) ((iecode) & 0x07)
  597. #define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
  598. #define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
  599. #define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
  600. #define FR_LMI_ANSI_PVC_STATUS_IE 0x07
  601. #define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
  602. #define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
  603. #define FR_LMI_CCITT_PVC_STATUS_IE 0x57
  604. static const struct tok fr_q933_ie_values_codeset_0_5[] = {
  605. { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
  606. { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
  607. { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
  608. { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
  609. { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
  610. { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
  611. { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
  612. { 0, NULL }
  613. };
  614. #define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
  615. #define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
  616. #define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
  617. static const struct tok fr_lmi_report_type_ie_values[] = {
  618. { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
  619. { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
  620. { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
  621. { 0, NULL }
  622. };
  623. /* array of 16 codesets - currently we only support codepage 0 and 5 */
  624. static const struct tok *fr_q933_ie_codesets[] = {
  625. fr_q933_ie_values_codeset_0_5,
  626. NULL,
  627. NULL,
  628. NULL,
  629. NULL,
  630. fr_q933_ie_values_codeset_0_5,
  631. NULL,
  632. NULL,
  633. NULL,
  634. NULL,
  635. NULL,
  636. NULL,
  637. NULL,
  638. NULL,
  639. NULL,
  640. NULL
  641. };
  642. static int fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
  643. u_int ielength, const u_char *p);
  644. typedef int (*codeset_pr_func_t)(netdissect_options *, u_int iecode,
  645. u_int ielength, const u_char *p);
  646. /* array of 16 codesets - currently we only support codepage 0 and 5 */
  647. static const codeset_pr_func_t fr_q933_print_ie_codeset[] = {
  648. fr_q933_print_ie_codeset_0_5,
  649. NULL,
  650. NULL,
  651. NULL,
  652. NULL,
  653. fr_q933_print_ie_codeset_0_5,
  654. NULL,
  655. NULL,
  656. NULL,
  657. NULL,
  658. NULL,
  659. NULL,
  660. NULL,
  661. NULL,
  662. NULL,
  663. NULL
  664. };
  665. /*
  666. * ITU-T Q.933.
  667. *
  668. * p points to octet 2, the octet containing the length of the
  669. * call reference value, so p[n] is octet n+2 ("octet X" is as
  670. * used in Q.931/Q.933).
  671. *
  672. * XXX - actually used both for Q.931 and Q.933.
  673. */
  674. void
  675. q933_print(netdissect_options *ndo,
  676. const u_char *p, u_int length)
  677. {
  678. u_int olen;
  679. u_int call_ref_length, i;
  680. uint8_t call_ref[15]; /* maximum length - length field is 4 bits */
  681. u_int msgtype;
  682. u_int iecode;
  683. u_int ielength;
  684. u_int codeset = 0;
  685. u_int is_ansi = 0;
  686. u_int ie_is_known;
  687. u_int non_locking_shift;
  688. u_int unshift_codeset;
  689. ND_PRINT((ndo, "%s", ndo->ndo_eflag ? "" : "Q.933"));
  690. if (length == 0 || !ND_TTEST(*p)) {
  691. if (!ndo->ndo_eflag)
  692. ND_PRINT((ndo, ", "));
  693. ND_PRINT((ndo, "length %u", length));
  694. goto trunc;
  695. }
  696. /*
  697. * Get the length of the call reference value.
  698. */
  699. olen = length; /* preserve the original length for display */
  700. call_ref_length = (*p) & 0x0f;
  701. p++;
  702. length--;
  703. /*
  704. * Get the call reference value.
  705. */
  706. for (i = 0; i < call_ref_length; i++) {
  707. if (length == 0 || !ND_TTEST(*p)) {
  708. if (!ndo->ndo_eflag)
  709. ND_PRINT((ndo, ", "));
  710. ND_PRINT((ndo, "length %u", olen));
  711. goto trunc;
  712. }
  713. call_ref[i] = *p;
  714. p++;
  715. length--;
  716. }
  717. /*
  718. * Get the message type.
  719. */
  720. if (length == 0 || !ND_TTEST(*p)) {
  721. if (!ndo->ndo_eflag)
  722. ND_PRINT((ndo, ", "));
  723. ND_PRINT((ndo, "length %u", olen));
  724. goto trunc;
  725. }
  726. msgtype = *p;
  727. p++;
  728. length--;
  729. /*
  730. * Peek ahead to see if we start with a shift.
  731. */
  732. non_locking_shift = 0;
  733. unshift_codeset = codeset;
  734. if (length != 0) {
  735. if (!ND_TTEST(*p)) {
  736. if (!ndo->ndo_eflag)
  737. ND_PRINT((ndo, ", "));
  738. ND_PRINT((ndo, "length %u", olen));
  739. goto trunc;
  740. }
  741. iecode = *p;
  742. if (IE_IS_SHIFT(iecode)) {
  743. /*
  744. * It's a shift. Skip over it.
  745. */
  746. p++;
  747. length--;
  748. /*
  749. * Get the codeset.
  750. */
  751. codeset = IE_SHIFT_CODESET(iecode);
  752. /*
  753. * If it's a locking shift to codeset 5,
  754. * mark this as ANSI. (XXX - 5 is actually
  755. * for national variants in general, not
  756. * the US variant in particular, but maybe
  757. * this is more American exceptionalism. :-))
  758. */
  759. if (IE_SHIFT_IS_LOCKING(iecode)) {
  760. /*
  761. * It's a locking shift.
  762. */
  763. if (codeset == 5) {
  764. /*
  765. * It's a locking shift to
  766. * codeset 5, so this is
  767. * T1.617 Annex D.
  768. */
  769. is_ansi = 1;
  770. }
  771. } else {
  772. /*
  773. * It's a non-locking shift.
  774. * Remember the current codeset, so we
  775. * can revert to it after the next IE.
  776. */
  777. non_locking_shift = 1;
  778. unshift_codeset = 0;
  779. }
  780. }
  781. }
  782. /* printing out header part */
  783. if (!ndo->ndo_eflag)
  784. ND_PRINT((ndo, ", "));
  785. ND_PRINT((ndo, "%s, codeset %u", is_ansi ? "ANSI" : "CCITT", codeset));
  786. if (call_ref_length != 0) {
  787. ND_TCHECK(p[0]);
  788. if (call_ref_length > 1 || p[0] != 0) {
  789. /*
  790. * Not a dummy call reference.
  791. */
  792. ND_PRINT((ndo, ", Call Ref: 0x"));
  793. for (i = 0; i < call_ref_length; i++)
  794. ND_PRINT((ndo, "%02x", call_ref[i]));
  795. }
  796. }
  797. if (ndo->ndo_vflag) {
  798. ND_PRINT((ndo, ", %s (0x%02x), length %u",
  799. tok2str(fr_q933_msg_values,
  800. "unknown message", msgtype),
  801. msgtype,
  802. olen));
  803. } else {
  804. ND_PRINT((ndo, ", %s",
  805. tok2str(fr_q933_msg_values,
  806. "unknown message 0x%02x", msgtype)));
  807. }
  808. /* Loop through the rest of the IEs */
  809. while (length != 0) {
  810. /*
  811. * What's the state of any non-locking shifts?
  812. */
  813. if (non_locking_shift == 1) {
  814. /*
  815. * There's a non-locking shift in effect for
  816. * this IE. Count it, so we reset the codeset
  817. * before the next IE.
  818. */
  819. non_locking_shift = 2;
  820. } else if (non_locking_shift == 2) {
  821. /*
  822. * Unshift.
  823. */
  824. codeset = unshift_codeset;
  825. non_locking_shift = 0;
  826. }
  827. /*
  828. * Get the first octet of the IE.
  829. */
  830. if (!ND_TTEST(*p)) {
  831. if (!ndo->ndo_vflag) {
  832. ND_PRINT((ndo, ", length %u", olen));
  833. }
  834. goto trunc;
  835. }
  836. iecode = *p;
  837. p++;
  838. length--;
  839. /* Single-octet IE? */
  840. if (IE_IS_SINGLE_OCTET(iecode)) {
  841. /*
  842. * Yes. Is it a shift?
  843. */
  844. if (IE_IS_SHIFT(iecode)) {
  845. /*
  846. * Yes. Is it locking?
  847. */
  848. if (IE_SHIFT_IS_LOCKING(iecode)) {
  849. /*
  850. * Yes.
  851. */
  852. non_locking_shift = 0;
  853. } else {
  854. /*
  855. * No. Remember the current
  856. * codeset, so we can revert
  857. * to it after the next IE.
  858. */
  859. non_locking_shift = 1;
  860. unshift_codeset = codeset;
  861. }
  862. /*
  863. * Get the codeset.
  864. */
  865. codeset = IE_SHIFT_CODESET(iecode);
  866. }
  867. } else {
  868. /*
  869. * No. Get the IE length.
  870. */
  871. if (length == 0 || !ND_TTEST(*p)) {
  872. if (!ndo->ndo_vflag) {
  873. ND_PRINT((ndo, ", length %u", olen));
  874. }
  875. goto trunc;
  876. }
  877. ielength = *p;
  878. p++;
  879. length--;
  880. /* lets do the full IE parsing only in verbose mode
  881. * however some IEs (DLCI Status, Link Verify)
  882. * are also interesting in non-verbose mode */
  883. if (ndo->ndo_vflag) {
  884. ND_PRINT((ndo, "\n\t%s IE (0x%02x), length %u: ",
  885. tok2str(fr_q933_ie_codesets[codeset],
  886. "unknown", iecode),
  887. iecode,
  888. ielength));
  889. }
  890. /* sanity checks */
  891. if (iecode == 0 || ielength == 0) {
  892. return;
  893. }
  894. if (length < ielength || !ND_TTEST2(*p, ielength)) {
  895. if (!ndo->ndo_vflag) {
  896. ND_PRINT((ndo, ", length %u", olen));
  897. }
  898. goto trunc;
  899. }
  900. ie_is_known = 0;
  901. if (fr_q933_print_ie_codeset[codeset] != NULL) {
  902. ie_is_known = fr_q933_print_ie_codeset[codeset](ndo, iecode, ielength, p);
  903. }
  904. if (ie_is_known) {
  905. /*
  906. * Known IE; do we want to see a hexdump
  907. * of it?
  908. */
  909. if (ndo->ndo_vflag > 1) {
  910. /* Yes. */
  911. print_unknown_data(ndo, p, "\n\t ", ielength);
  912. }
  913. } else {
  914. /*
  915. * Unknown IE; if we're printing verbosely,
  916. * print its content in hex.
  917. */
  918. if (ndo->ndo_vflag >= 1) {
  919. print_unknown_data(ndo, p, "\n\t", ielength);
  920. }
  921. }
  922. length -= ielength;
  923. p += ielength;
  924. }
  925. }
  926. if (!ndo->ndo_vflag) {
  927. ND_PRINT((ndo, ", length %u", olen));
  928. }
  929. return;
  930. trunc:
  931. ND_PRINT((ndo, "[|q.933]"));
  932. }
  933. static int
  934. fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
  935. u_int ielength, const u_char *p)
  936. {
  937. u_int dlci;
  938. switch (iecode) {
  939. case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
  940. case FR_LMI_CCITT_REPORT_TYPE_IE:
  941. if (ielength < 1) {
  942. if (!ndo->ndo_vflag) {
  943. ND_PRINT((ndo, ", "));
  944. }
  945. ND_PRINT((ndo, "Invalid REPORT TYPE IE"));
  946. return 1;
  947. }
  948. if (ndo->ndo_vflag) {
  949. ND_PRINT((ndo, "%s (%u)",
  950. tok2str(fr_lmi_report_type_ie_values,"unknown",p[0]),
  951. p[0]));
  952. }
  953. return 1;
  954. case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
  955. case FR_LMI_CCITT_LINK_VERIFY_IE:
  956. case FR_LMI_ANSI_LINK_VERIFY_IE_91:
  957. if (!ndo->ndo_vflag) {
  958. ND_PRINT((ndo, ", "));
  959. }
  960. if (ielength < 2) {
  961. ND_PRINT((ndo, "Invalid LINK VERIFY IE"));
  962. return 1;
  963. }
  964. ND_PRINT((ndo, "TX Seq: %3d, RX Seq: %3d", p[0], p[1]));
  965. return 1;
  966. case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
  967. case FR_LMI_CCITT_PVC_STATUS_IE:
  968. if (!ndo->ndo_vflag) {
  969. ND_PRINT((ndo, ", "));
  970. }
  971. /* now parse the DLCI information element. */
  972. if ((ielength < 3) ||
  973. (p[0] & 0x80) ||
  974. ((ielength == 3) && !(p[1] & 0x80)) ||
  975. ((ielength == 4) && ((p[1] & 0x80) || !(p[2] & 0x80))) ||
  976. ((ielength == 5) && ((p[1] & 0x80) || (p[2] & 0x80) ||
  977. !(p[3] & 0x80))) ||
  978. (ielength > 5) ||
  979. !(p[ielength - 1] & 0x80)) {
  980. ND_PRINT((ndo, "Invalid DLCI in PVC STATUS IE"));
  981. return 1;
  982. }
  983. dlci = ((p[0] & 0x3F) << 4) | ((p[1] & 0x78) >> 3);
  984. if (ielength == 4) {
  985. dlci = (dlci << 6) | ((p[2] & 0x7E) >> 1);
  986. }
  987. else if (ielength == 5) {
  988. dlci = (dlci << 13) | (p[2] & 0x7F) | ((p[3] & 0x7E) >> 1);
  989. }
  990. ND_PRINT((ndo, "DLCI %u: status %s%s", dlci,
  991. p[ielength - 1] & 0x8 ? "New, " : "",
  992. p[ielength - 1] & 0x2 ? "Active" : "Inactive"));
  993. return 1;
  994. }
  995. return 0;
  996. }
  997. /*
  998. * Local Variables:
  999. * c-style: whitesmith
  1000. * c-basic-offset: 8
  1001. * End:
  1002. */