1 /* osi-utils.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly
3  * Main entrance point and common functions
4  *
5  * Laurent Deniel <laurent.deniel@free.fr>
6  * Ralf Schneider <Ralf.Schneider@t-online.de>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14 
15 #include "config.h"
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <glib.h>
20 
21 #include "tvbuff.h"
22 #include "osi-utils.h"
23 #include "address.h"
24 #include "address_types.h"
25 
26 static void print_nsap_net_buf( const guint8 *, int, gchar *, int);
27 static void print_area_buf ( const guint8 *, int, gchar *, int);
28 static void print_address_prefix_buf ( const guint8 *, int, gchar *, int);
29 
30 /*
31  * XXX - shouldn't there be a centralized routine for dissecting NSAPs?
32  * See also "dissect_atm_nsap()" in epan/dissectors/packet-arp.c and
33  * "dissect_nsap()" in epan/dissectors/packet-isup.c.
34  */
35 gchar *
print_nsap_net(wmem_allocator_t * scope,tvbuff_t * tvb,const gint offset,int length)36 print_nsap_net( wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, int length )
37 {
38   gchar *cur;
39 
40   cur = (gchar *)wmem_alloc(scope, MAX_NSAP_LEN * 3 + 50);
41   print_nsap_net_buf( tvb_get_ptr(tvb, offset, length), length, cur, MAX_NSAP_LEN * 3 + 50);
42   return( cur );
43 }
44 
45 static void
print_nsap_net_buf(const guint8 * ad,int length,gchar * buf,int buf_len)46 print_nsap_net_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
47 {
48   gchar *cur;
49 
50   /* to do : NSAP / NET decoding */
51 
52   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
53     (void) g_strlcpy(buf, "<Invalid length of NSAP>", buf_len);
54     return;
55   }
56   cur = buf;
57   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
58     print_area_buf(ad, RFC1237_FULLAREA_LEN, cur, buf_len);
59     cur += strlen( cur );
60     print_system_id_buf( ad + RFC1237_FULLAREA_LEN, RFC1237_SYSTEMID_LEN, cur, (int) (buf_len-(cur-buf)));
61     cur += strlen( cur );
62     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x]",
63                     ad[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
64     if ( length == RFC1237_NSAP_LEN + 1 ) {
65       g_snprintf(cur, (int) (buf_len-(cur-buf)), "-%02x", ad[ length -1 ] );
66     }
67   }
68   else {    /* probably format as standard */
69     /* XXX - this is an NSAP, not an area address/address prefix */
70     print_area_buf( ad, length, buf, buf_len);
71   }
72 } /* print_nsap */
73 
74 gchar *
print_system_id(wmem_allocator_t * scope,const guint8 * ad,int length)75 print_system_id(wmem_allocator_t* scope, const guint8 *ad, int length )
76 {
77   gchar        *cur;
78 
79   cur = (gchar *)wmem_alloc(scope, MAX_SYSTEMID_LEN * 3 + 5);
80   print_system_id_buf(ad, length, cur, MAX_SYSTEMID_LEN * 3 + 5);
81   return( cur );
82 }
83 
84 gchar *
tvb_print_system_id(wmem_allocator_t * scope,tvbuff_t * tvb,const gint offset,int length)85 tvb_print_system_id( wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, int length )
86 {
87   return( print_system_id(scope, tvb_get_ptr(tvb, offset, length), length) );
88 }
89 
90 void
print_system_id_buf(const guint8 * ad,int length,gchar * buf,int buf_len)91 print_system_id_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
92 {
93   gchar        *cur;
94   int           tmp;
95 
96   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
97     (void) g_strlcpy(buf, "<Invalid length of SYSTEM ID>", buf_len);
98     return;
99   }
100 
101   cur = buf;
102   if ( ( 6 == length ) || /* System-ID */
103        ( 7 == length ) || /* LAN-ID */
104        ( 8 == length )) { /* LSP-ID */
105     cur += g_snprintf(cur, buf_len, "%02x%02x.%02x%02x.%02x%02x", ad[0], ad[1],
106                     ad[2], ad[3], ad[4], ad[5] );
107     if ( ( 7 == length ) ||
108          ( 8 == length )) {
109         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[6] );
110     }
111     if ( 8 == length ) {
112         g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[7] );
113     }
114   }
115   else {
116     tmp = 0;
117     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
118       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
119       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
120       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
121       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
122     }
123     if ( 1 == tmp ) {   /* Special case for Designated IS */
124       cur--;
125       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[tmp] );
126     }
127     else {
128       for ( ; tmp < length; ) {  /* print the rest without dot */
129         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
130       }
131     }
132   }
133 }
134 
135 gchar *
print_area(wmem_allocator_t * scope,tvbuff_t * tvb,const gint offset,int length)136 print_area(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, int length)
137 {
138   gchar *cur;
139 
140   cur = (gchar *)wmem_alloc(scope, MAX_AREA_LEN * 3 + 20);
141   print_area_buf(tvb_get_ptr(tvb, offset, length), length, cur, MAX_AREA_LEN * 3 + 20);
142   return cur;
143 }
144 
145 /*
146  * Note: length is in units of half-octets.
147  */
148 gchar *
print_address_prefix(wmem_allocator_t * scope,tvbuff_t * tvb,const gint offset,int length)149 print_address_prefix(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, int length)
150 {
151   gchar *cur;
152 
153   cur = (gchar *)wmem_alloc(scope, MAX_AREA_LEN * 3 + 20);
154   print_address_prefix_buf(tvb_get_ptr(tvb, offset, (length+1)/2), length, cur, MAX_AREA_LEN * 3 + 20);
155   return cur;
156 }
157 
158 /*
159  * Note: length is in units of octets.
160  */
161 static void
print_area_buf(const guint8 * ad,int length,gchar * buf,int buf_len)162 print_area_buf(const guint8 *ad, int length, gchar *buf, int buf_len)
163 {
164   print_address_prefix_buf(ad, length*2, buf, buf_len);
165 }
166 
167 /*
168  * Note: length is in units of half-octets.
169  */
170 static void
print_address_prefix_buf(const guint8 * ad,int length,gchar * buf,int buf_len)171 print_address_prefix_buf(const guint8 *ad, int length, gchar *buf, int buf_len)
172 {
173   gchar *cur;
174   int  tmp  = 0;
175 
176   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
177    */
178   if (length <= 0 || length > MAX_AREA_LEN*2) {
179     (void) g_strlcpy(buf, "<Invalid length of AREA>", buf_len);
180     return;
181   }
182 
183   cur = buf;
184   /* Check the AFI and length. */
185   if ( (  ( NSAP_IDI_ISO_DCC_BIN      == *ad )
186        || ( NSAP_IDI_ISO_6523_ICD_BIN == *ad )
187        )
188        &&
189        (  ( RFC1237_FULLAREA_LEN*2       ==  length )
190        || ( (RFC1237_FULLAREA_LEN + 1)*2 ==  length )
191        )
192      ) {    /* AFI is good and length is long enough  */
193 
194     /* The AFI is either ISO DCC, binary or ISO 6523-ICD, binary,
195      * and the area length corresponds either to the area length
196      * for RFC 1237 (GOSIP) addresses or that length + 1.
197      *
198      * XXX - RFC 1237 doesn't mention ISO DCC, binary, as a valid
199      * AFI; is that from GOSIP Version 1?  If it's ISO DCC, binary,
200      * the IDI is 3 digits, i.e. 1 1/2 octets.
201      */
202     /* there used to be a check for (length > RFC1237_FULLAREA_LEN + 1) here,
203      * in order to report an invalied length of AREA for DCC / ISO 6523 AFI,
204      * but that can *never* be the case because the if() test above explicitly
205      * tests for (length == RFC1237_FULLAREA_LEN) or (length == RFC1237_FULLAREA_LEN + 1)
206      */
207 
208     /* Show the one-octet AFI, the two-octet IDI, the one-octet DFI, the
209      * 3-octet AA, and the 2 reserved octets.
210      */
211     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]",
212                     ad[0], ad[1], ad[2], ad[3], ad[4],
213                     ad[5], ad[6], ad[7], ad[8] );
214     /* Show the 2-octet RD and the 2-octet Area. */
215     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x:%02x|%02x:%02x]",
216                     ad[9], ad[10],  ad[11], ad[12] );
217     /* Show whatever the heck this is; it's not specified by RFC 1237,
218      * but we also handle 14-octet areas.  Is it the "Designated IS"
219      * stuff mentioned below?  (I didn't find anything in the IS-IS
220      * spec about that.)
221      */
222     if ( (RFC1237_FULLAREA_LEN + 1)*2 == length )
223       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-[%02x]", ad[13] );
224   }
225   else {
226     /* This doesn't look like a full RFC 1237 IS-IS area, so all we know
227      * is that the first octet is an AFI.  Print it separately from all
228      * the other octets.
229      */
230     if ( length == RFC1237_AREA_LEN*2 ) {
231       /* XXX - RFC1237_AREA_LEN, which is 3 octets, doesn't seem to
232        * correspond to anything in RFC 1237.  Where did it come from?
233        */
234       g_snprintf(buf, buf_len, "%02x.%02x%02x", ad[0], ad[1], ad[2] );
235       return;
236     }
237     if ( length == 4*2 ) {
238       g_snprintf(buf, buf_len, "%02x%02x%02x%02x", ad[0], ad[1], ad[2], ad[3] );
239       return;
240     }
241     while ( tmp < length / 8 ) {      /* 32/8==4 > four Octets left to print */
242       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
243       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
244       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
245       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
246     }
247     if ( 2 == tmp ) {                     /* Special case for Designated IS */
248       cur--;
249       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[tmp] );
250     }
251     else {
252       for ( ; tmp < length / 2; ) {  /* print the rest without dot or dash */
253         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
254       }
255       /* Odd half-octet? */
256       if (length & 1) {
257         /* Yes - print it (it's the upper half-octet) */
258         g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%x", (ad[tmp] & 0xF0)>>4 );
259       }
260     }
261   }
262 } /* print_address_prefix_buf */
263 
264 /******************************************************************************
265  * OSI Address Type
266  ******************************************************************************/
267 static int osi_address_type = -1;
268 
osi_address_to_str(const address * addr,gchar * buf,int buf_len)269 static int osi_address_to_str(const address* addr, gchar *buf, int buf_len)
270 {
271     print_nsap_net_buf((const guint8 *)addr->data, addr->len, buf, buf_len);
272     return (int)strlen(buf)+1;
273 }
274 
osi_address_str_len(const address * addr _U_)275 static int osi_address_str_len(const address* addr _U_)
276 {
277     return MAX_NSAP_LEN * 3 + 50;
278 }
279 
get_osi_address_type(void)280 int get_osi_address_type(void)
281 {
282     return osi_address_type;
283 }
284 
register_osi_address_type(void)285 void register_osi_address_type(void)
286 {
287     if (osi_address_type != -1)
288         return;
289 
290     osi_address_type = address_type_dissector_register("AT_OSI", "OSI Address", osi_address_to_str, osi_address_str_len, NULL, NULL, NULL, NULL, NULL);
291 }
292 
293 
294 /*
295  * Editor modelines
296  *
297  * Local Variables:
298  * c-basic-offset: 2
299  * tab-width: 8
300  * indent-tabs-mode: nil
301  * End:
302  *
303  * ex: set shiftwidth=2 tabstop=8 expandtab:
304  * :indentSize=2:tabSize=8:noTabs=true:
305  */
306