]> git.sur5r.net Git - ngadmin/blob - wireshark/nsdp.lua
Cli: disable all non batch mode code when building without readline support
[ngadmin] / wireshark / nsdp.lua
1
2 p_nsdp = Proto("nsdp", "Netgear Switch Description Protocol")
3
4 local f_version = ProtoField.uint8("nsdp.version", "Version", base.DEC)
5 local f_code = ProtoField.uint8("nsdp.code", "Operation Code", base.DEC)
6 local f_error = ProtoField.uint8("nsdp.error", "Error Code", base.DEC)
7 local f_errattr = ProtoField.uint16("nsdp.errattr", "Erroneous Attribute", base.HEX)
8 local f_clientmac = ProtoField.ether("nsdp.clientmac", "Client MAC")
9 local f_switchmac = ProtoField.ether("nsdp.switchmac", "Switch MAC")
10 local f_seqnum = ProtoField.uint32("nsdp.seqnum", "Sequence Number", base.DEC)
11
12 p_nsdp.fields = {
13         f_version,
14         f_code,
15         f_error,
16         f_errattr,
17         f_clientmac,
18         f_switchmac,
19         f_seqnum
20 }
21
22
23
24 local op_codes = {
25         [1] = "Read Request",
26         [2] = "Read Reply",
27         [3] = "Write Request",
28         [4] = "Write Reply"
29 }
30
31
32 local error_codes = {
33         [0] = "OK",
34         [5] = "Invalid Value",
35         [7] = "Access Denied"
36 }
37
38
39 local status_codes = {
40         [0] = "down",
41         [1] = "10M",
42         [4] = "100M",
43         [5] = "1000M"
44 }
45
46
47 local bitrates_codes = {
48         [0] = "unlimited",
49         [1] = "512K",
50         [2] = "1M",
51         [3] = "2M",
52         [4] = "4M",
53         [5] = "8M",
54         [6] = "16M",
55         [7] = "32M",
56         [8] = "64M",
57         [9] = "128M",
58         [10] = "256M",
59         [11] = "512M"
60 }
61
62
63 local vlan_type_codes = {
64         [0] = "disabled",
65         [1] = "port basic",
66         [2] = "port advanced",
67         [3] = "802.1Q basic",
68         [4] = "802.1Q advanced"
69 }
70
71
72 local qos_type_codes = {
73         [1] = "port based",
74         [2] = "802.1p"
75 }
76
77
78 local prio_codes = {
79         [1] = "high",
80         [2] = "medium",
81         [3] = "normal",
82         [4] = "low"
83 }
84
85
86
87
88 local function dissect_port_statistics(buffer, offset, subtree)
89         subtree:add(buffer(offset + 4, 1), string.format("Port: %i", buffer(offset + 4, 1):uint()))
90         subtree:add(buffer(offset + 4 + 1 + 8 * 0, 8), "Received:", tostring(buffer(offset + 4 + 1 + 8 * 0, 8):uint64()))
91         subtree:add(buffer(offset + 4 + 1 + 8 * 1, 8), "Sent: ", tostring(buffer(offset + 4 + 1 + 8 * 1, 8):uint64()))
92         subtree:add(buffer(offset + 4 + 1 + 8 * 5, 8), "CRC Errors:", tostring(buffer(offset + 4 + 1 + 8 * 5, 8):uint64()))
93 end
94
95
96 local function dissect_port_status(buffer, offset, subtree)
97         local st = buffer(offset + 5, 1):uint()
98         subtree:add(buffer(offset + 4, 1), string.format("Port: %i", buffer(offset + 4, 1):uint()))
99         subtree:add(buffer(offset + 5, 1), string.format("Status: %i (%s)", st, status_codes[st] or "unk"))
100 end
101
102
103 local function dissect_qos_type(buffer, offset, subtree)
104         local t = buffer(offset + 4, 1):uint()
105         subtree:add(buffer(offset + 4, 1), string.format("QoS Type: %i (%s)", t, qos_type_codes[t] or "unk"))
106 end
107
108
109 local function dissect_qos_config(buffer, offset, subtree)
110         local p = buffer(offset + 5, 1):uint()
111         subtree:add(buffer(offset + 4, 1), string.format("Port: %i", buffer(offset + 4, 1):uint()))
112         subtree:add(buffer(offset + 5, 1), string.format("Priority: %i (%s)", p, prio_codes[p] or "unk"))
113 end
114
115
116 local function dissect_bitrate(buffer, offset, subtree)
117         local sp = buffer(offset + 5, 4):uint()
118         subtree:add(buffer(offset + 4, 1), string.format("Port: %i", buffer(offset + 4, 1):uint()))
119         subtree:add(buffer(offset + 5, 4), string.format("Speed: %i (%s)", sp, bitrates_codes[sp] or "unk"))
120 end
121
122
123 local function dissect_vlan_type(buffer, offset, subtree)
124         local vt = buffer(offset + 4, 1):uint()
125         subtree:add(buffer(offset + 4, 1), string.format("VLAN Type: %i (%s)", vt, vlan_type_codes[vt] or "unk"))
126 end
127
128
129 local function parse_ports(val)
130         local ports = ""
131         
132         for i = 8, 1, -1 do
133                 if (val % 2 == 1) then
134                         ports = ports..i.." "
135                 end
136                 val = math.floor(val / 2)
137         end
138         
139         return ports
140 end
141
142
143 local function dissect_vlan_port_conf(buffer, offset, subtree)
144         subtree:add(buffer(offset + 4, 2), string.format("VLAN: %u", buffer(offset + 4, 2):uint()))
145         
146         if (buffer(offset + 2, 2):uint() >= 3) then
147                 subtree:add(buffer(offset + 6, 1), "Ports:", parse_ports(buffer(offset + 6, 1):uint()))
148         end
149 end
150
151
152 local function dissect_vlan_8021q_conf(buffer, offset, subtree)
153         subtree:add(buffer(offset + 4, 2), string.format("VLAN: %u", buffer(offset + 4, 2):uint()))
154         
155         if (buffer(offset + 2, 2):uint() >= 4) then
156                 subtree:add(buffer(offset + 6, 1), "Ports:", parse_ports(buffer(offset + 6, 1):uint()))
157                 subtree:add(buffer(offset + 7, 1), "Tagged Ports:", parse_ports(buffer(offset + 7, 1):uint()))
158         end
159 end
160
161
162 local function dissect_vlan_pvid(buffer, offset, subtree)
163         subtree:add(buffer(offset + 4, 1), string.format("Port: %i", buffer(offset + 4, 1):uint()))
164         subtree:add(buffer(offset + 5, 2), string.format("VLAN: %u", buffer(offset + 5, 2):uint()))
165 end
166
167
168 local function dissect_mirror(buffer, offset, subtree)
169         local op = buffer(offset + 4, 1):uint()
170         
171         if (op == 0) then
172                 subtree:add(buffer(offset + 4, 1), "Disabled")
173         else
174                 subtree:add(buffer(offset + 4, 1), "Output Port:", op)
175                 subtree:add(buffer(offset + 6, 1), "Ports:", parse_ports(buffer(offset + 6, 1):uint()))
176         end
177 end
178
179
180 local function dissect_igmp_enablevlan(buffer, offset, subtree)
181         subtree:add(buffer(offset + 4, 2), string.format("Enable: %u", buffer(offset + 4, 2):uint()))
182         subtree:add(buffer(offset + 6, 2), string.format("VLAN: %u", buffer(offset + 6, 2):uint()))
183 end
184
185
186 local attributes = {
187         [0x0001] = {name = "Product", dissect = "string"}, 
188         [0x0003] = {name = "Name", dissect = "string"}, 
189         [0x0004] = {name = "MAC", dissect = "ether"}, 
190         [0x0006] = {name = "IP", dissect = "ipv4"}, 
191         [0x0007] = {name = "Mask", dissect = "ipv4"}, 
192         [0x0008] = {name = "Gateway", dissect = "ipv4"}, 
193         [0x0009] = {name = "New Password", dissect = "string"}, 
194         [0x000A] = {name = "Password", dissect = "string"}, 
195         [0x000B] = {name = "DHCP", dissect = "uint"}, 
196         [0x000D] = {name = "Firmware Version", dissect = "string"}, 
197         [0x0010] = {name = "Firmware Upgrade", dissect = "uint"}, 
198         [0x0013] = {name = "Restart", dissect = "uint"}, 
199         [0x0014] = {name = "Encrypt Passwords", dissect = "uint"}, 
200         [0x0400] = {name = "Defaults", dissect = "uint"}, 
201         [0x0C00] = {name = "Port Status", dissect = dissect_port_status}, 
202         [0x1000] = {name = "Port Statistics", dissect = dissect_port_statistics}, 
203         [0x1400] = {name = "Reset Ports Statistics", dissect = "uint"}, 
204         [0x1800] = {name = "Cabletest Do", dissect = nill}, 
205         [0x1C00] = {name = "Cabletest Result", dissect = nill}, 
206         [0x2000] = {name = "VLAN Type", dissect=dissect_vlan_type}, 
207         [0x2400] = {name = "VLAN Port Conf", dissect=dissect_vlan_port_conf}, 
208         [0x2800] = {name = "VLAN 802.1Q Conf", dissect=dissect_vlan_8021q_conf}, 
209         [0x2C00] = {name = "Destroy VLAN", dissect = "uint"}, 
210         [0x3000] = {name = "VLAN PVID", dissect = dissect_vlan_pvid}, 
211         [0x3400] = {name = "QoS Type", dissect = dissect_qos_type}, 
212         [0x3800] = {name = "QoS Config", dissect = dissect_qos_config}, 
213         [0x4C00] = {name = "Input Bitrate", dissect = dissect_bitrate}, 
214         [0x5000] = {name = "Output Bitrate", dissect = dissect_bitrate}, 
215         [0x5400] = {name = "Broadcast Filtering State", dissect = "uint"}, 
216         [0x5800] = {name = "Broadcast Filtering Bitrate", dissect = dissect_bitrate}, 
217         [0x5C00] = {name = "Mirror", dissect = dissect_mirror}, 
218         [0x6000] = {name = "Ports Count", dissect = "uint"}, 
219         [0x6400] = {name = "Max 802.1Q VLAN Group", dissect = nill}, 
220         [0x6800] = {name = "IGMP Enable & VLAN", dissect = dissect_igmp_enablevlan}, 
221         [0x6C00] = {name = "Block Unknown IGMP Addresses", dissect = "uint"}, 
222         [0x7000] = {name = "Validate IGMPv3 Headers", dissect = "uint"}, 
223         [0x7400] = {name = "TLV Bitmap", dissect = nill}, 
224         [0xFFFF] = {name = "End", dissect = nill}
225 }
226
227
228
229 local function dissect_header(buffer, subtree)
230         subtree:add(f_version, buffer(0, 1))
231         subtree:add(f_code, buffer(1, 1)):append_text(" ("..(op_codes[buffer(1, 1):uint()] or "unknown")..")")
232         
233         local errcode = buffer(2, 1):uint()
234         local errattr = buffer(4, 2):uint()
235         subtree:add(f_error, buffer(2, 1)):append_text(" ("..(error_codes[errcode] or "unknown")..")")
236         
237         -- add the erroneous attribute only if an error occurred
238         if (errattr ~= 0) then
239                 local atf = attributes[errattr]
240                 subtree:add(f_errattr, buffer(4, 2)):append_text(" ("..(atf and atf.name or "unk")..")")
241         end
242         
243         subtree:add(f_clientmac, buffer(8, 6))
244         subtree:add(f_switchmac, buffer(14, 6))
245         subtree:add(f_seqnum, buffer(20, 4))
246 end
247
248
249 local function dissect_attributes(buffer, subtree)
250         local offset = 32
251         
252         while (offset < buffer:len()) do
253                 if (offset + 4 > buffer:len()) then
254                         -- no room for an attribute header, it is an error
255                         subtree:add(buffer(offset), "Junk"):set_expert_flags(PI_MALFORMED, PI_ERROR)
256                         break
257                 end
258                 
259                 local code = buffer(offset, 2):uint()
260                 local len = buffer(offset + 2, 2):uint()
261                 local atf = attributes[code]
262                 
263                 local attr = subtree:add(buffer(offset, math.min(4 + len, buffer:len() - offset)), string.format("Attribute: 0x%04X (%s)", code, atf and atf.name or "unk"))
264                 attr:add(buffer(offset, 2), string.format("Code: 0x%04X", code))
265                 attr:add(buffer(offset + 2, 2), string.format("Length: %u", len))
266                 
267                 if (offset + 4 + len > buffer:len()) then
268                         -- attribute length is bigger than remaining packet size, it is an error
269                         attr:append_text(" [malformed]")
270                         attr:set_expert_flags(PI_MALFORMED, PI_ERROR)
271                         break
272                 end
273                 
274                 
275                 if (len <= 0) then
276                         -- no data, display nothing
277                 elseif (atf == nil or atf.dissect == nil) then
278                         -- unknown attribute, display raw bytes
279                         attr:add(buffer(offset + 4, len), "Data:", tostring(buffer(offset + 4, len):bytes()))
280                 elseif (type(atf.dissect) == "function" ) then
281                         -- custom sub-dissector for complex type
282                         atf.dissect(buffer, offset, attr)
283                 else
284                         -- simple type, directly show it
285                         local func = assert(loadstring("return function(buffer, offset, len) return tostring(buffer(offset + 4, len):"..atf.dissect.."()) end"))() -- ugly, isn't it ?
286                         attr:add(buffer(offset + 4, len), atf.name..":", func(buffer, offset, len))
287                 end
288                 
289                 offset = offset + 4 + len
290         end
291 end
292
293
294 function p_nsdp.dissector(buffer, pinfo, tree)
295         pinfo.cols.protocol = p_nsdp.name
296         local subtree = tree:add(p_nsdp, buffer())
297         
298         -- stop if the packet is too small to be valid
299         if (buffer:len() < 32) then
300                 return
301         end
302         
303         dissect_header(buffer, subtree)
304         
305         -- stop if it is just a header
306         if (buffer:len() == 32) then
307                 return
308         end
309         
310         local attr_list = subtree:add(buffer(32), "Attributes list")
311         dissect_attributes(buffer, attr_list)
312 end
313
314
315 function p_nsdp.init ()
316 end
317
318
319 local udp_dissector_table = DissectorTable.get("udp.port")
320 udp_dissector_table:add(63322, p_nsdp)
321 udp_dissector_table:add(63321, p_nsdp)
322
323