Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 1 | diff --git a/scapy/contrib/bier.py b/scapy/contrib/bier.py |
| 2 | new file mode 100644 |
| 3 | index 0000000..e173cdb |
| 4 | --- /dev/null |
| 5 | +++ b/scapy/contrib/bier.py |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 6 | @@ -0,0 +1,58 @@ |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 7 | +# http://trac.secdev.org/scapy/ticket/31 |
| 8 | + |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 9 | +# scapy.contrib.description = BIER |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 10 | +# scapy.contrib.status = loads |
| 11 | + |
| 12 | +from scapy.packet import * |
| 13 | +from scapy.fields import * |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 14 | +from scapy.layers.inet import IP, UDP |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 15 | +from scapy.layers.inet6 import IPv6 |
| 16 | + |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 17 | + |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 18 | +class BIERLength: |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 19 | + BIER_LEN_64 = 0 |
| 20 | + BIER_LEN_128 = 1 |
| 21 | + BIER_LEN_256 = 2 |
| 22 | + BIER_LEN_512 = 3 |
| 23 | + BIER_LEN_1024 = 4 |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 24 | + |
| 25 | + |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 26 | +BIERnhcls = {1: "MPLS", |
| 27 | + 2: "MPLS", |
| 28 | + 4: "IPv4", |
| 29 | + 5: "IPv6"} |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 30 | + |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 31 | + |
| 32 | +class BIFT(Packet): |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 33 | + name = "BIFT" |
| 34 | + fields_desc = [BitField("bsl", 0, 4), |
| 35 | + BitField("sd", 0, 8), |
| 36 | + BitField("set", 0, 8), |
| 37 | + BitField("cos", 0, 3), |
| 38 | + BitField("s", 1, 1), |
| 39 | + ByteField("ttl", 0)] |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 40 | + |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 41 | + def guess_payload_class(self, payload): |
| 42 | + return BIER |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 43 | + |
| 44 | + |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 45 | +class BIER(Packet): |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 46 | + name = "BIER" |
| 47 | + fields_desc = [BitField("id", 5, 4), |
| 48 | + BitField("version", 0, 4), |
| 49 | + BitFieldLenField("length", BIERLength.BIER_LEN_256, 4, |
| 50 | + length_of=lambda x:(x.BitString >> 8)), |
| 51 | + BitField("entropy", 0, 20), |
| 52 | + BitField("OAM", 0, 2), |
| 53 | + BitField("RSV", 0, 2), |
| 54 | + BitField("DSCP", 0, 6), |
| 55 | + BitEnumField("Proto", 2, 6, BIERnhcls), |
| 56 | + ShortField("BFRID", 0), |
| 57 | + StrLenField("BitString", |
| 58 | + "", |
| 59 | + length_from=lambda x:(8 << x.length))] |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 60 | + |
| 61 | + |
| 62 | +bind_layers(BIER, IP, Proto=4) |
| 63 | +bind_layers(BIER, IPv6, Proto=5) |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 64 | +bind_layers(UDP, BIFT, dport=8138) |