Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/scapy/layers/ppp.py: 86%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

300 statements  

1# SPDX-License-Identifier: GPL-2.0-only 

2# This file is part of Scapy 

3# See https://scapy.net/ for more information 

4# Copyright (C) Philippe Biondi <phil@secdev.org> 

5 

6""" 

7PPP (Point to Point Protocol) 

8 

9[RFC 1661] 

10""" 

11 

12import struct 

13from scapy.config import conf 

14from scapy.data import DLT_PPP, DLT_PPP_SERIAL, DLT_PPP_ETHER, \ 

15 DLT_PPP_WITH_DIR 

16from scapy.packet import Packet, bind_layers 

17from scapy.layers.eap import EAP 

18from scapy.layers.l2 import Ether, CookedLinux, GRE_PPTP 

19from scapy.layers.inet import IP 

20from scapy.layers.inet6 import IPv6 

21from scapy.fields import ( 

22 BitField, 

23 ByteEnumField, 

24 ByteField, 

25 ConditionalField, 

26 EnumField, 

27 FieldLenField, 

28 IPField, 

29 IntField, 

30 OUIField, 

31 PacketField, 

32 PacketListField, 

33 ShortEnumField, 

34 ShortField, 

35 StrLenField, 

36 XByteField, 

37 XShortField, 

38 XStrLenField, 

39) 

40 

41 

42class PPPoE(Packet): 

43 name = "PPP over Ethernet" 

44 fields_desc = [BitField("version", 1, 4), 

45 BitField("type", 1, 4), 

46 ByteEnumField("code", 0, {0: "Session"}), 

47 XShortField("sessionid", 0x0), 

48 ShortField("len", None)] 

49 

50 def post_build(self, p, pay): 

51 p += pay 

52 if self.len is None: 

53 tmp_len = len(p) - 6 

54 p = p[:4] + struct.pack("!H", tmp_len) + p[6:] 

55 return p 

56 

57 

58# PPPoE Active Discovery Code fields (RFC2516, RFC5578) 

59class PPPoED(PPPoE): 

60 name = "PPP over Ethernet Discovery" 

61 

62 code_list = {0x00: "PPP Session Stage", 

63 0x09: "PPPoE Active Discovery Initiation (PADI)", 

64 0x07: "PPPoE Active Discovery Offer (PADO)", 

65 0x0a: "PPPoE Active Discovery Session-Grant (PADG)", 

66 0x0b: "PPPoE Active Discovery Session-Credit Response (PADC)", 

67 0x0c: "PPPoE Active Discovery Quality (PADQ)", 

68 0x19: "PPPoE Active Discovery Request (PADR)", 

69 0x65: "PPPoE Active Discovery Session-confirmation (PADS)", 

70 0xa7: "PPPoE Active Discovery Terminate (PADT)"} 

71 

72 fields_desc = [BitField("version", 1, 4), 

73 BitField("type", 1, 4), 

74 ByteEnumField("code", 0x09, code_list), 

75 XShortField("sessionid", 0x0), 

76 ShortField("len", None)] 

77 

78 def extract_padding(self, s): 

79 return s[:self.len], s[self.len:] 

80 

81 def mysummary(self): 

82 return self.sprintf("%code%") 

83 

84 

85# PPPoE Tag types (RFC2516, RFC4638, RFC5578) 

86class PPPoETag(Packet): 

87 name = "PPPoE Tag" 

88 

89 tag_list = {0x0000: 'End-Of-List', 

90 0x0101: 'Service-Name', 

91 0x0102: 'AC-Name', 

92 0x0103: 'Host-Uniq', 

93 0x0104: 'AC-Cookie', 

94 0x0105: 'Vendor-Specific', 

95 0x0106: 'Credits', 

96 0x0107: 'Metrics', 

97 0x0108: 'Sequence Number', 

98 0x0109: 'Credit Scale Factor', 

99 0x0110: 'Relay-Session-Id', 

100 0x0120: 'PPP-Max-Payload', 

101 0x0201: 'Service-Name-Error', 

102 0x0202: 'AC-System-Error', 

103 0x0203: 'Generic-Error'} 

104 

105 fields_desc = [ 

106 ShortEnumField('tag_type', None, tag_list), 

107 FieldLenField('tag_len', None, length_of='tag_value', fmt='H'), 

108 StrLenField('tag_value', '', length_from=lambda pkt:pkt.tag_len) 

109 ] 

110 

111 def extract_padding(self, s): 

112 return '', s 

113 

114 

115class PPPoED_Tags(Packet): 

116 name = "PPPoE Tag List" 

117 fields_desc = [PacketListField('tag_list', None, PPPoETag)] 

118 

119 def mysummary(self): 

120 return "PPPoE Tags" + ", ".join( 

121 x.sprintf("%tag_type%") for x in self.tag_list 

122 ), [PPPoED] 

123 

124 

125_PPP_PROTOCOLS = { 

126 0x0001: "Padding Protocol", 

127 0x0003: "ROHC small-CID [RFC3095]", 

128 0x0005: "ROHC large-CID [RFC3095]", 

129 0x0021: "Internet Protocol version 4", 

130 0x0023: "OSI Network Layer", 

131 0x0025: "Xerox NS IDP", 

132 0x0027: "DECnet Phase IV", 

133 0x0029: "Appletalk", 

134 0x002b: "Novell IPX", 

135 0x002d: "Van Jacobson Compressed TCP/IP", 

136 0x002f: "Van Jacobson Uncompressed TCP/IP", 

137 0x0031: "Bridging PDU", 

138 0x0033: "Stream Protocol (ST-II)", 

139 0x0035: "Banyan Vines", 

140 0x0037: "reserved (until 1993) [Typo in RFC1172]", 

141 0x0039: "AppleTalk EDDP", 

142 0x003b: "AppleTalk SmartBuffered", 

143 0x003d: "Multi-Link [RFC1717]", 

144 0x003f: "NETBIOS Framing", 

145 0x0041: "Cisco Systems", 

146 0x0043: "Ascom Timeplex", 

147 0x0045: "Fujitsu Link Backup and Load Balancing (LBLB)", 

148 0x0047: "DCA Remote Lan", 

149 0x0049: "Serial Data Transport Protocol (PPP-SDTP)", 

150 0x004b: "SNA over 802.2", 

151 0x004d: "SNA", 

152 0x004f: "IPv6 Header Compression", 

153 0x0051: "KNX Bridging Data [ianp]", 

154 0x0053: "Encryption [Meyer]", 

155 0x0055: "Individual Link Encryption [Meyer]", 

156 0x0057: "Internet Protocol version 6 [Hinden]", 

157 0x0059: "PPP Muxing [RFC3153]", 

158 0x005b: "Vendor-Specific Network Protocol (VSNP) [RFC3772]", 

159 0x0061: "RTP IPHC Full Header [RFC3544]", 

160 0x0063: "RTP IPHC Compressed TCP [RFC3544]", 

161 0x0065: "RTP IPHC Compressed Non TCP [RFC3544]", 

162 0x0067: "RTP IPHC Compressed UDP 8 [RFC3544]", 

163 0x0069: "RTP IPHC Compressed RTP 8 [RFC3544]", 

164 0x006f: "Stampede Bridging", 

165 0x0071: "Reserved [Fox]", 

166 0x0073: "MP+ Protocol [Smith]", 

167 0x007d: "reserved (Control Escape) [RFC1661]", 

168 0x007f: "reserved (compression inefficient [RFC1662]", 

169 0x0081: "Reserved Until 20-Oct-2000 [IANA]", 

170 0x0083: "Reserved Until 20-Oct-2000 [IANA]", 

171 0x00c1: "NTCITS IPI [Ungar]", 

172 0x00cf: "reserved (PPP NLID)", 

173 0x00fb: "single link compression in multilink [RFC1962]", 

174 0x00fd: "compressed datagram [RFC1962]", 

175 0x00ff: "reserved (compression inefficient)", 

176 0x0201: "802.1d Hello Packets", 

177 0x0203: "IBM Source Routing BPDU", 

178 0x0205: "DEC LANBridge100 Spanning Tree", 

179 0x0207: "Cisco Discovery Protocol [Sastry]", 

180 0x0209: "Netcs Twin Routing [Korfmacher]", 

181 0x020b: "STP - Scheduled Transfer Protocol [Segal]", 

182 0x020d: "EDP - Extreme Discovery Protocol [Grosser]", 

183 0x0211: "Optical Supervisory Channel Protocol (OSCP)[Prasad]", 

184 0x0213: "Optical Supervisory Channel Protocol (OSCP)[Prasad]", 

185 0x0231: "Luxcom", 

186 0x0233: "Sigma Network Systems", 

187 0x0235: "Apple Client Server Protocol [Ridenour]", 

188 0x0281: "MPLS Unicast [RFC3032] ", 

189 0x0283: "MPLS Multicast [RFC3032]", 

190 0x0285: "IEEE p1284.4 standard - data packets [Batchelder]", 

191 0x0287: "ETSI TETRA Network Protocol Type 1 [Nieminen]", 

192 0x0289: "Multichannel Flow Treatment Protocol [McCann]", 

193 0x2063: "RTP IPHC Compressed TCP No Delta [RFC3544]", 

194 0x2065: "RTP IPHC Context State [RFC3544]", 

195 0x2067: "RTP IPHC Compressed UDP 16 [RFC3544]", 

196 0x2069: "RTP IPHC Compressed RTP 16 [RFC3544]", 

197 0x4001: "Cray Communications Control Protocol [Stage]", 

198 0x4003: "CDPD Mobile Network Registration Protocol [Quick]", 

199 0x4005: "Expand accelerator protocol [Rachmani]", 

200 0x4007: "ODSICP NCP [Arvind]", 

201 0x4009: "DOCSIS DLL [Gaedtke]", 

202 0x400B: "Cetacean Network Detection Protocol [Siller]", 

203 0x4021: "Stacker LZS [Simpson]", 

204 0x4023: "RefTek Protocol [Banfill]", 

205 0x4025: "Fibre Channel [Rajagopal]", 

206 0x4027: "EMIT Protocols [Eastham]", 

207 0x405b: "Vendor-Specific Protocol (VSP) [RFC3772]", 

208 0x8021: "Internet Protocol Control Protocol", 

209 0x8023: "OSI Network Layer Control Protocol", 

210 0x8025: "Xerox NS IDP Control Protocol", 

211 0x8027: "DECnet Phase IV Control Protocol", 

212 0x8029: "Appletalk Control Protocol", 

213 0x802b: "Novell IPX Control Protocol", 

214 0x802d: "reserved", 

215 0x802f: "reserved", 

216 0x8031: "Bridging NCP", 

217 0x8033: "Stream Protocol Control Protocol", 

218 0x8035: "Banyan Vines Control Protocol", 

219 0x8037: "reserved (until 1993)", 

220 0x8039: "reserved", 

221 0x803b: "reserved", 

222 0x803d: "Multi-Link Control Protocol", 

223 0x803f: "NETBIOS Framing Control Protocol", 

224 0x8041: "Cisco Systems Control Protocol", 

225 0x8043: "Ascom Timeplex", 

226 0x8045: "Fujitsu LBLB Control Protocol", 

227 0x8047: "DCA Remote Lan Network Control Protocol (RLNCP)", 

228 0x8049: "Serial Data Control Protocol (PPP-SDCP)", 

229 0x804b: "SNA over 802.2 Control Protocol", 

230 0x804d: "SNA Control Protocol", 

231 0x804f: "IP6 Header Compression Control Protocol", 

232 0x8051: "KNX Bridging Control Protocol [ianp]", 

233 0x8053: "Encryption Control Protocol [Meyer]", 

234 0x8055: "Individual Link Encryption Control Protocol [Meyer]", 

235 0x8057: "IPv6 Control Protovol [Hinden]", 

236 0x8059: "PPP Muxing Control Protocol [RFC3153]", 

237 0x805b: "Vendor-Specific Network Control Protocol (VSNCP) [RFC3772]", 

238 0x806f: "Stampede Bridging Control Protocol", 

239 0x8073: "MP+ Control Protocol [Smith]", 

240 0x8071: "Reserved [Fox]", 

241 0x807d: "Not Used - reserved [RFC1661]", 

242 0x8081: "Reserved Until 20-Oct-2000 [IANA]", 

243 0x8083: "Reserved Until 20-Oct-2000 [IANA]", 

244 0x80c1: "NTCITS IPI Control Protocol [Ungar]", 

245 0x80cf: "Not Used - reserved [RFC1661]", 

246 0x80fb: "single link compression in multilink control [RFC1962]", 

247 0x80fd: "Compression Control Protocol [RFC1962]", 

248 0x80ff: "Not Used - reserved [RFC1661]", 

249 0x8207: "Cisco Discovery Protocol Control [Sastry]", 

250 0x8209: "Netcs Twin Routing [Korfmacher]", 

251 0x820b: "STP - Control Protocol [Segal]", 

252 0x820d: "EDPCP - Extreme Discovery Protocol Ctrl Prtcl [Grosser]", 

253 0x8235: "Apple Client Server Protocol Control [Ridenour]", 

254 0x8281: "MPLSCP [RFC3032]", 

255 0x8285: "IEEE p1284.4 standard - Protocol Control [Batchelder]", 

256 0x8287: "ETSI TETRA TNP1 Control Protocol [Nieminen]", 

257 0x8289: "Multichannel Flow Treatment Protocol [McCann]", 

258 0xc021: "Link Control Protocol", 

259 0xc023: "Password Authentication Protocol", 

260 0xc025: "Link Quality Report", 

261 0xc027: "Shiva Password Authentication Protocol", 

262 0xc029: "CallBack Control Protocol (CBCP)", 

263 0xc02b: "BACP Bandwidth Allocation Control Protocol [RFC2125]", 

264 0xc02d: "BAP [RFC2125]", 

265 0xc05b: "Vendor-Specific Authentication Protocol (VSAP) [RFC3772]", 

266 0xc081: "Container Control Protocol [KEN]", 

267 0xc223: "Challenge Handshake Authentication Protocol", 

268 0xc225: "RSA Authentication Protocol [Narayana]", 

269 0xc227: "Extensible Authentication Protocol [RFC2284]", 

270 0xc229: "Mitsubishi Security Info Exch Ptcl (SIEP) [Seno]", 

271 0xc26f: "Stampede Bridging Authorization Protocol", 

272 0xc281: "Proprietary Authentication Protocol [KEN]", 

273 0xc283: "Proprietary Authentication Protocol [Tackabury]", 

274 0xc481: "Proprietary Node ID Authentication Protocol [KEN]", 

275} 

276 

277 

278class HDLC(Packet): 

279 fields_desc = [XByteField("address", 0xff), 

280 XByteField("control", 0x03)] 

281 

282 

283# LINKTYPE_PPP_WITH_DIR 

284class DIR_PPP(Packet): 

285 fields_desc = [ByteEnumField("direction", 0, ["received", "sent"])] 

286 

287 

288class _PPPProtoField(EnumField): 

289 """ 

290 A field that can be either Byte or Short, depending on the PPP RFC. 

291 

292 See RFC 1661 section 2 

293 <https://tools.ietf.org/html/rfc1661#section-2> 

294 

295 The generated proto field is two bytes when not specified, or when specified 

296 as an integer or a string: 

297 PPP() 

298 PPP(proto=0x21) 

299 PPP(proto="Internet Protocol version 4") 

300 To explicitly forge a one byte proto field, use the bytes representation: 

301 PPP(proto=b'\x21') 

302 """ 

303 def getfield(self, pkt, s): 

304 if ord(s[:1]) & 0x01: 

305 self.fmt = "!B" 

306 self.sz = 1 

307 else: 

308 self.fmt = "!H" 

309 self.sz = 2 

310 self.struct = struct.Struct(self.fmt) 

311 return super(_PPPProtoField, self).getfield(pkt, s) 

312 

313 def addfield(self, pkt, s, val): 

314 if isinstance(val, bytes): 

315 if len(val) == 1: 

316 fmt, sz = "!B", 1 

317 elif len(val) == 2: 

318 fmt, sz = "!H", 2 

319 else: 

320 raise TypeError('Invalid length for PPP proto') 

321 val = struct.Struct(fmt).unpack(val)[0] 

322 else: 

323 fmt, sz = "!H", 2 

324 self.fmt = fmt 

325 self.sz = sz 

326 self.struct = struct.Struct(self.fmt) 

327 return super(_PPPProtoField, self).addfield(pkt, s, val) 

328 

329 

330class PPP(Packet): 

331 name = "PPP Link Layer" 

332 fields_desc = [_PPPProtoField("proto", 0x0021, _PPP_PROTOCOLS)] 

333 

334 @classmethod 

335 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

336 if _pkt and _pkt[:1] == b'\xff': 

337 return HDLC 

338 return cls 

339 

340 

341_PPP_conftypes = {1: "Configure-Request", 

342 2: "Configure-Ack", 

343 3: "Configure-Nak", 

344 4: "Configure-Reject", 

345 5: "Terminate-Request", 

346 6: "Terminate-Ack", 

347 7: "Code-Reject", 

348 8: "Protocol-Reject", 

349 9: "Echo-Request", 

350 10: "Echo-Reply", 

351 11: "Discard-Request", 

352 14: "Reset-Request", 

353 15: "Reset-Ack", 

354 } 

355 

356 

357# PPP IPCP stuff (RFC 1332) 

358 

359# All IPCP options are defined below (names and associated classes) 

360_PPP_ipcpopttypes = {1: "IP-Addresses (Deprecated)", 

361 2: "IP-Compression-Protocol", 

362 3: "IP-Address", 

363 # not implemented, present for completeness 

364 4: "Mobile-IPv4", 

365 129: "Primary-DNS-Address", 

366 130: "Primary-NBNS-Address", 

367 131: "Secondary-DNS-Address", 

368 132: "Secondary-NBNS-Address"} 

369 

370 

371class PPP_IPCP_Option(Packet): 

372 name = "PPP IPCP Option" 

373 fields_desc = [ 

374 ByteEnumField("type", None, _PPP_ipcpopttypes), 

375 FieldLenField("len", None, length_of="data", fmt="B", 

376 adjust=lambda _, val: val + 2), 

377 StrLenField("data", "", length_from=lambda pkt: max(0, pkt.len - 2)), 

378 ] 

379 

380 def extract_padding(self, pay): 

381 return b"", pay 

382 

383 registered_options = {} 

384 

385 @classmethod 

386 def register_variant(cls): 

387 cls.registered_options[cls.type.default] = cls 

388 

389 @classmethod 

390 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

391 if _pkt: 

392 o = _pkt[0] 

393 return cls.registered_options.get(o, cls) 

394 return cls 

395 

396 

397class PPP_IPCP_Option_IPAddress(PPP_IPCP_Option): 

398 name = "PPP IPCP Option: IP Address" 

399 fields_desc = [ 

400 ByteEnumField("type", 3, _PPP_ipcpopttypes), 

401 FieldLenField("len", None, length_of="data", fmt="B", 

402 adjust=lambda _, val: val + 2), 

403 IPField("data", "0.0.0.0"), 

404 StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6), 

405 ] 

406 

407 

408class PPP_IPCP_Option_DNS1(PPP_IPCP_Option_IPAddress): 

409 name = "PPP IPCP Option: DNS1 Address" 

410 type = 129 

411 

412 

413class PPP_IPCP_Option_DNS2(PPP_IPCP_Option_IPAddress): 

414 name = "PPP IPCP Option: DNS2 Address" 

415 type = 131 

416 

417 

418class PPP_IPCP_Option_NBNS1(PPP_IPCP_Option_IPAddress): 

419 name = "PPP IPCP Option: NBNS1 Address" 

420 type = 130 

421 

422 

423class PPP_IPCP_Option_NBNS2(PPP_IPCP_Option_IPAddress): 

424 name = "PPP IPCP Option: NBNS2 Address" 

425 type = 132 

426 

427 

428class PPP_IPCP(Packet): 

429 fields_desc = [ 

430 ByteEnumField("code", 1, _PPP_conftypes), 

431 XByteField("id", 0), 

432 FieldLenField("len", None, fmt="H", length_of="options", 

433 adjust=lambda _, val: val + 4), 

434 PacketListField("options", [], PPP_IPCP_Option, 

435 length_from=lambda pkt: pkt.len - 4) 

436 ] 

437 

438 

439# ECP 

440 

441_PPP_ecpopttypes = {0: "OUI", 

442 1: "DESE", } 

443 

444 

445class PPP_ECP_Option(Packet): 

446 name = "PPP ECP Option" 

447 fields_desc = [ 

448 ByteEnumField("type", None, _PPP_ecpopttypes), 

449 FieldLenField("len", None, length_of="data", fmt="B", 

450 adjust=lambda _, val: val + 2), 

451 StrLenField("data", "", length_from=lambda pkt: max(0, pkt.len - 2)), 

452 ] 

453 

454 def extract_padding(self, pay): 

455 return b"", pay 

456 

457 registered_options = {} 

458 

459 @classmethod 

460 def register_variant(cls): 

461 cls.registered_options[cls.type.default] = cls 

462 

463 @classmethod 

464 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

465 if _pkt: 

466 o = _pkt[0] 

467 return cls.registered_options.get(o, cls) 

468 return cls 

469 

470 

471class PPP_ECP_Option_OUI(PPP_ECP_Option): 

472 fields_desc = [ 

473 ByteEnumField("type", 0, _PPP_ecpopttypes), 

474 FieldLenField("len", None, length_of="data", fmt="B", 

475 adjust=lambda _, val: val + 6), 

476 OUIField("oui", 0), 

477 ByteField("subtype", 0), 

478 StrLenField("data", "", length_from=lambda pkt: pkt.len - 6), 

479 ] 

480 

481 

482class PPP_ECP(Packet): 

483 fields_desc = [ 

484 ByteEnumField("code", 1, _PPP_conftypes), 

485 XByteField("id", 0), 

486 FieldLenField("len", None, fmt="H", length_of="options", 

487 adjust=lambda _, val: val + 4), 

488 PacketListField("options", [], PPP_ECP_Option, 

489 length_from=lambda pkt: pkt.len - 4), 

490 ] 

491 

492# Link Control Protocol (RFC 1661) 

493 

494 

495_PPP_lcptypes = {1: "Configure-Request", 

496 2: "Configure-Ack", 

497 3: "Configure-Nak", 

498 4: "Configure-Reject", 

499 5: "Terminate-Request", 

500 6: "Terminate-Ack", 

501 7: "Code-Reject", 

502 8: "Protocol-Reject", 

503 9: "Echo-Request", 

504 10: "Echo-Reply", 

505 11: "Discard-Request"} 

506 

507 

508class PPP_LCP(Packet): 

509 name = "PPP Link Control Protocol" 

510 fields_desc = [ 

511 ByteEnumField("code", 5, _PPP_lcptypes), 

512 XByteField("id", 0), 

513 FieldLenField("len", None, fmt="H", length_of="data", 

514 adjust=lambda _, val: val + 4), 

515 StrLenField("data", "", length_from=lambda pkt: pkt.len - 4), 

516 ] 

517 

518 def mysummary(self): 

519 return self.sprintf('LCP %code%') 

520 

521 def extract_padding(self, pay): 

522 return b"", pay 

523 

524 @classmethod 

525 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

526 if _pkt: 

527 o = _pkt[0] 

528 if o in [1, 2, 3, 4]: 

529 return PPP_LCP_Configure 

530 elif o in [5, 6]: 

531 return PPP_LCP_Terminate 

532 elif o == 7: 

533 return PPP_LCP_Code_Reject 

534 elif o == 8: 

535 return PPP_LCP_Protocol_Reject 

536 elif o in [9, 10]: 

537 return PPP_LCP_Echo 

538 elif o == 11: 

539 return PPP_LCP_Discard_Request 

540 else: 

541 return cls 

542 return cls 

543 

544 

545_PPP_lcp_optiontypes = {1: "Maximum-Receive-Unit", 

546 2: "Async-Control-Character-Map", 

547 3: "Authentication-protocol", 

548 4: "Quality-protocol", 

549 5: "Magic-number", 

550 7: "Protocol-Field-Compression", 

551 8: "Address-and-Control-Field-Compression", 

552 13: "Callback"} 

553 

554 

555class PPP_LCP_Option(Packet): 

556 name = "PPP LCP Option" 

557 fields_desc = [ 

558 ByteEnumField("type", None, _PPP_lcp_optiontypes), 

559 FieldLenField("len", None, fmt="B", length_of="data", 

560 adjust=lambda _, val: val + 2), 

561 StrLenField("data", None, length_from=lambda pkt: pkt.len - 2), 

562 ] 

563 

564 def extract_padding(self, pay): 

565 return b"", pay 

566 

567 registered_options = {} 

568 

569 @classmethod 

570 def register_variant(cls): 

571 cls.registered_options[cls.type.default] = cls 

572 

573 @classmethod 

574 def dispatch_hook(cls, _pkt=None, *args, **kargs): 

575 if _pkt: 

576 o = _pkt[0] 

577 return cls.registered_options.get(o, cls) 

578 return cls 

579 

580 

581class PPP_LCP_MRU_Option(PPP_LCP_Option): 

582 fields_desc = [ByteEnumField("type", 1, _PPP_lcp_optiontypes), 

583 ByteField("len", 4), 

584 ShortField("max_recv_unit", 1500)] 

585 

586 

587_PPP_LCP_auth_protocols = { 

588 0xc023: "Password authentication protocol", 

589 0xc223: "Challenge-response authentication protocol", 

590 0xc227: "PPP Extensible authentication protocol", 

591} 

592 

593_PPP_LCP_CHAP_algorithms = { 

594 5: "MD5", 

595 6: "SHA1", 

596 128: "MS-CHAP", 

597 129: "MS-CHAP-v2", 

598} 

599 

600 

601class PPP_LCP_ACCM_Option(PPP_LCP_Option): 

602 fields_desc = [ 

603 ByteEnumField("type", 2, _PPP_lcp_optiontypes), 

604 ByteField("len", 6), 

605 BitField("accm", 0x00000000, 32), 

606 ] 

607 

608 

609def adjust_auth_len(pkt, x): 

610 if pkt.auth_protocol == 0xc223: 

611 return 5 

612 elif pkt.auth_protocol == 0xc023: 

613 return 4 

614 else: 

615 return x + 4 

616 

617 

618class PPP_LCP_Auth_Protocol_Option(PPP_LCP_Option): 

619 fields_desc = [ 

620 ByteEnumField("type", 3, _PPP_lcp_optiontypes), 

621 FieldLenField("len", None, fmt="B", length_of="data", 

622 adjust=adjust_auth_len), 

623 ShortEnumField("auth_protocol", 0xc023, _PPP_LCP_auth_protocols), 

624 ConditionalField( 

625 StrLenField("data", '', length_from=lambda pkt: pkt.len - 4), 

626 lambda pkt: pkt.auth_protocol != 0xc223 

627 ), 

628 ConditionalField( 

629 ByteEnumField("algorithm", 5, _PPP_LCP_CHAP_algorithms), 

630 lambda pkt: pkt.auth_protocol == 0xc223 

631 ), 

632 ] 

633 

634 

635_PPP_LCP_quality_protocols = {0xc025: "Link Quality Report"} 

636 

637 

638class PPP_LCP_Quality_Protocol_Option(PPP_LCP_Option): 

639 fields_desc = [ 

640 ByteEnumField("type", 4, _PPP_lcp_optiontypes), 

641 FieldLenField("len", None, fmt="B", length_of="data", 

642 adjust=lambda _, val: val + 4), 

643 ShortEnumField("quality_protocol", 0xc025, _PPP_LCP_quality_protocols), 

644 StrLenField("data", "", length_from=lambda pkt: pkt.len - 4), 

645 ] 

646 

647 

648class PPP_LCP_Magic_Number_Option(PPP_LCP_Option): 

649 fields_desc = [ 

650 ByteEnumField("type", 5, _PPP_lcp_optiontypes), 

651 ByteField("len", 6), 

652 IntField("magic_number", None), 

653 ] 

654 

655 

656_PPP_lcp_callback_operations = { 

657 0: "Location determined by user authentication", 

658 1: "Dialing string", 

659 2: "Location identifier", 

660 3: "E.164 number", 

661 4: "Distinguished name", 

662} 

663 

664 

665class PPP_LCP_Callback_Option(PPP_LCP_Option): 

666 fields_desc = [ 

667 ByteEnumField("type", 13, _PPP_lcp_optiontypes), 

668 FieldLenField("len", None, fmt="B", length_of="message", 

669 adjust=lambda _, val: val + 3), 

670 ByteEnumField("operation", 0, _PPP_lcp_callback_operations), 

671 StrLenField("message", "", length_from=lambda pkt: pkt.len - 3) 

672 ] 

673 

674 

675class PPP_LCP_Configure(PPP_LCP): 

676 fields_desc = [ 

677 ByteEnumField("code", 1, _PPP_lcptypes), 

678 XByteField("id", 0), 

679 FieldLenField("len", None, fmt="H", length_of="options", 

680 adjust=lambda _, val: val + 4), 

681 PacketListField("options", [], PPP_LCP_Option, 

682 length_from=lambda pkt: pkt.len - 4), 

683 ] 

684 

685 def answers(self, other): 

686 return ( 

687 isinstance(other, PPP_LCP_Configure) and self.code in [2, 3, 4] and 

688 other.code == 1 and other.id == self.id 

689 ) 

690 

691 

692class PPP_LCP_Terminate(PPP_LCP): 

693 

694 def answers(self, other): 

695 return ( 

696 isinstance(other, PPP_LCP_Terminate) and self.code == 6 and 

697 other.code == 5 and other.id == self.id 

698 ) 

699 

700 

701class PPP_LCP_Code_Reject(PPP_LCP): 

702 fields_desc = [ 

703 ByteEnumField("code", 7, _PPP_lcptypes), 

704 XByteField("id", 0), 

705 FieldLenField("len", None, fmt="H", length_of="rejected_packet", 

706 adjust=lambda _, val: val + 4), 

707 PacketField("rejected_packet", None, PPP_LCP), 

708 ] 

709 

710 

711class PPP_LCP_Protocol_Reject(PPP_LCP): 

712 fields_desc = [ 

713 ByteEnumField("code", 8, _PPP_lcptypes), 

714 XByteField("id", 0), 

715 FieldLenField("len", None, fmt="H", length_of="rejected_information", 

716 adjust=lambda _, val: val + 6), 

717 ShortEnumField("rejected_protocol", None, _PPP_PROTOCOLS), 

718 PacketField("rejected_information", None, Packet), 

719 ] 

720 

721 

722class PPP_LCP_Discard_Request(PPP_LCP): 

723 fields_desc = [ 

724 ByteEnumField("code", 11, _PPP_lcptypes), 

725 XByteField("id", 0), 

726 FieldLenField("len", None, fmt="H", length_of="data", 

727 adjust=lambda _, val: val + 8), 

728 IntField("magic_number", None), 

729 StrLenField("data", "", length_from=lambda pkt: pkt.len - 8), 

730 ] 

731 

732 

733class PPP_LCP_Echo(PPP_LCP_Discard_Request): 

734 code = 9 

735 

736 def answers(self, other): 

737 return ( 

738 isinstance(other, PPP_LCP_Echo) and self.code == 10 and 

739 other.code == 9 and self.id == other.id 

740 ) 

741 

742 

743# Password authentication protocol (RFC 1334) 

744 

745 

746_PPP_paptypes = {1: "Authenticate-Request", 

747 2: "Authenticate-Ack", 

748 3: "Authenticate-Nak"} 

749 

750 

751class PPP_PAP(Packet): 

752 name = "PPP Password Authentication Protocol" 

753 fields_desc = [ 

754 ByteEnumField("code", 1, _PPP_paptypes), 

755 XByteField("id", 0), 

756 FieldLenField("len", None, fmt="!H", length_of="data", 

757 adjust=lambda _, val: val + 4), 

758 StrLenField("data", "", length_from=lambda pkt: pkt.len - 4), 

759 ] 

760 

761 @classmethod 

762 def dispatch_hook(cls, _pkt=None, *_, **kargs): 

763 code = None 

764 if _pkt: 

765 code = _pkt[0] 

766 elif "code" in kargs: 

767 code = kargs["code"] 

768 if isinstance(code, str): 

769 code = cls.fields_desc[0].s2i[code] 

770 

771 if code == 1: 

772 return PPP_PAP_Request 

773 elif code in [2, 3]: 

774 return PPP_PAP_Response 

775 return cls 

776 

777 def extract_padding(self, pay): 

778 return "", pay 

779 

780 

781class PPP_PAP_Request(PPP_PAP): 

782 fields_desc = [ 

783 ByteEnumField("code", 1, _PPP_paptypes), 

784 XByteField("id", 0), 

785 FieldLenField("len", None, fmt="!H", length_of="username", 

786 adjust=lambda pkt, val: val + 6 + len(pkt.password)), 

787 FieldLenField("username_len", None, fmt="B", length_of="username"), 

788 StrLenField("username", None, 

789 length_from=lambda pkt: pkt.username_len), 

790 FieldLenField("passwd_len", None, fmt="B", length_of="password"), 

791 StrLenField("password", None, length_from=lambda pkt: pkt.passwd_len), 

792 ] 

793 

794 def mysummary(self): 

795 return self.sprintf("PAP-Request username=%PPP_PAP_Request.username%" 

796 " password=%PPP_PAP_Request.password%") 

797 

798 

799class PPP_PAP_Response(PPP_PAP): 

800 fields_desc = [ 

801 ByteEnumField("code", 2, _PPP_paptypes), 

802 XByteField("id", 0), 

803 FieldLenField("len", None, fmt="!H", length_of="message", 

804 adjust=lambda _, val: val + 5), 

805 FieldLenField("msg_len", None, fmt="B", length_of="message"), 

806 StrLenField("message", "", length_from=lambda pkt: pkt.msg_len), 

807 ] 

808 

809 def answers(self, other): 

810 return isinstance(other, PPP_PAP_Request) and other.id == self.id 

811 

812 def mysummary(self): 

813 res = "PAP-Ack" if self.code == 2 else "PAP-Nak" 

814 if self.msg_len > 0: 

815 res += self.sprintf(" msg=%PPP_PAP_Response.message%") 

816 return res 

817 

818 

819# Challenge Handshake Authentication protocol (RFC1994) 

820 

821_PPP_chaptypes = {1: "Challenge", 

822 2: "Response", 

823 3: "Success", 

824 4: "Failure"} 

825 

826 

827class PPP_CHAP(Packet): 

828 name = "PPP Challenge Handshake Authentication Protocol" 

829 fields_desc = [ 

830 ByteEnumField("code", 1, _PPP_chaptypes), 

831 XByteField("id", 0), 

832 FieldLenField("len", None, fmt="!H", length_of="data", 

833 adjust=lambda _, val: val + 4), 

834 StrLenField("data", "", length_from=lambda pkt: pkt.len - 4), 

835 ] 

836 

837 def answers(self, other): 

838 return isinstance(other, PPP_CHAP_ChallengeResponse) \ 

839 and other.code == 2 and self.code in (3, 4) \ 

840 and self.id == other.id 

841 

842 @classmethod 

843 def dispatch_hook(cls, _pkt=None, *_, **kargs): 

844 code = None 

845 if _pkt: 

846 code = _pkt[0] 

847 elif "code" in kargs: 

848 code = kargs["code"] 

849 if isinstance(code, str): 

850 code = cls.fields_desc[0].s2i[code] 

851 

852 if code in (1, 2): 

853 return PPP_CHAP_ChallengeResponse 

854 return cls 

855 

856 def extract_padding(self, pay): 

857 return "", pay 

858 

859 def mysummary(self): 

860 if self.code == 3: 

861 return self.sprintf("CHAP Success message=%PPP_CHAP.data%") 

862 elif self.code == 4: 

863 return self.sprintf("CHAP Failure message=%PPP_CHAP.data%") 

864 

865 

866class PPP_CHAP_ChallengeResponse(PPP_CHAP): 

867 fields_desc = [ 

868 ByteEnumField("code", 1, _PPP_chaptypes), 

869 XByteField("id", 0), 

870 FieldLenField( 

871 "len", None, fmt="!H", length_of="value", 

872 adjust=lambda pkt, val: val + len(pkt.optional_name) + 5, 

873 ), 

874 FieldLenField("value_size", None, fmt="B", length_of="value"), 

875 XStrLenField("value", b'\x00\x00\x00\x00\x00\x00\x00\x00', 

876 length_from=lambda pkt: pkt.value_size), 

877 StrLenField("optional_name", "", 

878 length_from=lambda pkt: pkt.len - pkt.value_size - 5), 

879 ] 

880 

881 def answers(self, other): 

882 return isinstance(other, PPP_CHAP_ChallengeResponse) \ 

883 and other.code == 1 and self.code == 2 and self.id == other.id 

884 

885 def mysummary(self): 

886 if self.code == 1: 

887 return self.sprintf( 

888 "CHAP challenge=0x%PPP_CHAP_ChallengeResponse.value% " 

889 "optional_name=%PPP_CHAP_ChallengeResponse.optional_name%" 

890 ) 

891 elif self.code == 2: 

892 return self.sprintf( 

893 "CHAP response=0x%PPP_CHAP_ChallengeResponse.value% " 

894 "optional_name=%PPP_CHAP_ChallengeResponse.optional_name%" 

895 ) 

896 else: 

897 return super(PPP_CHAP_ChallengeResponse, self).mysummary() 

898 

899 

900bind_layers(PPPoED, PPPoED_Tags, type=1) 

901bind_layers(Ether, PPPoED, type=0x8863) 

902bind_layers(Ether, PPPoE, type=0x8864) 

903bind_layers(CookedLinux, PPPoED, proto=0x8863) 

904bind_layers(CookedLinux, PPPoE, proto=0x8864) 

905bind_layers(PPPoE, PPP, code=0) 

906bind_layers(HDLC, PPP,) 

907bind_layers(DIR_PPP, PPP) 

908bind_layers(PPP, EAP, proto=0xc227) 

909bind_layers(PPP, IP, proto=0x0021) 

910bind_layers(PPP, IPv6, proto=0x0057) 

911bind_layers(PPP, PPP_CHAP, proto=0xc223) 

912bind_layers(PPP, PPP_IPCP, proto=0x8021) 

913bind_layers(PPP, PPP_ECP, proto=0x8053) 

914bind_layers(PPP, PPP_LCP, proto=0xc021) 

915bind_layers(PPP, PPP_PAP, proto=0xc023) 

916bind_layers(Ether, PPP_IPCP, type=0x8021) 

917bind_layers(Ether, PPP_ECP, type=0x8053) 

918bind_layers(GRE_PPTP, PPP, proto=0x880b) 

919 

920 

921conf.l2types.register(DLT_PPP, PPP) 

922conf.l2types.register(DLT_PPP_SERIAL, HDLC) 

923conf.l2types.register(DLT_PPP_ETHER, PPPoE) 

924conf.l2types.register(DLT_PPP_WITH_DIR, DIR_PPP)