-3

I am working with SDN floodlight controller and python sockets. I need 2 bits in the header field of tcp or ip header that I can use, header field that I can modify and set as I want.

I want to send packets with a custom field (2 bits or more) in either the tcp or ip header. I have used the script attached below for crafting a TCP header but was not able find any room for setting bits for my purpose.

One potiential solution I found was that there are 6 bits reserved in the tcp header but I could not set the reserved bits.

Any discussion or answer would help. Thank you

Credits to Suraj Singh for the code: http://www.bitforestinfo.com/2017/12/code-to-create-tcp-packet-header-with-python-socket-module.html

 def create_tcp_feilds(self):

  # ---- [ Source Port ]

  self.tcp_src = self.sport

  # ---- [ Destination Port ]

  self.tcp_dst = self.dport

  # ---- [ TCP Sequence Number]

  self.tcp_seq = 0

  # ---- [ TCP Acknowledgement Number]

  self.tcp_ack_seq = 0

  # ---- [ Header Length ]

  self.tcp_hdr_len = 80

  # ---- [ TCP Flags ]

tcp_flags_rsv = (0 << 9) # <<< cant set this field

tcp_flags_noc = (0 << 8)

  tcp_flags_cwr = (0 << 7)

  tcp_flags_ecn = (0 << 6)

  tcp_flags_urg = (0 << 5)

  tcp_flags_ack = (0 << 4)

  tcp_flags_psh = (0 << 3)

  tcp_flags_rst = (0 << 2)

  tcp_flags_syn = (1 << 1)

  tcp_flags_fin = (0)

  self.tcp_flags_rsv=1

  self.tcp_flags = tcp_flags_rsv + tcp_flags_noc + tcp_flags_cwr + \
        tcp_flags_ecn + tcp_flags_urg + tcp_flags_ack + \
        tcp_flags_psh + tcp_flags_rst + tcp_flags_syn + tcp_flags_fin

  self.tcp_flags = 255

  print(self.tcp_flags)

  print(bin(self.tcp_flags))

  print(tcp_flags_rsv)

  # ---- [ TCP Window Size ]
  self.tcp_wdw = socket.htons (5840)

  # ---- [ TCP CheckSum ]
  self.tcp_chksum = 0

  # ---- [ TCP Urgent Pointer ]  
self.tcp_urg_ptr = 0


  return 
  • 2
    Neither IP nor TCP were designed for such a thing. There are three (not six) reserved bits in the TCP header, and they are required to be set to `0`. Also, remember that the IPv4 and IPv6 headers are very different, so trying to do something in one would not be possible in the other. – Ron Maupin Feb 09 '20 at 20:28
  • 2
    You may be dealing with an [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378), so explaining what you really want to accomplish may lead to an answer on how to accomplish that. – Ron Maupin Feb 09 '20 at 20:30
  • Have you considered sending it in the payload? – user207421 Feb 09 '20 at 21:52
  • @user207421 Yes I used the payload for sending the bits but my professor had an objection on that he says that it has to be in the header. Otherwise there would be too much traffic towards the controller. – Hanzala Jamash Feb 13 '20 at 06:25

2 Answers2

1

I used the socket option to set the TOS last 2 bits known as ECN. Thanks to @Sascha for the insight.

s.setsockopt(socket.SOL_IP,socket.IP_TOS,0x03)

Using this function I can set the header TOS field without crafting raw sockets.

0

What about the Type Of Service (TOS) field, i.e. the second byte of the IPv4 header?

Nowadays, the first six bits of the field are interpreted as DSCP (Differentiated Services Code Point), the last two bits are interpreted as ECN (Explicit Congestion Notification). The ECN is an optional feature, so if your endpoints do not evaluate it (most probably they won’t), you can “highjack” these two bits for your use case.

Good thing about it is that you should be able to set the TOS (and thereby the ECN) on application level.

You can find more information about it here: https://en.wikipedia.org/wiki/Type_of_service and here: https://en.wikipedia.org/wiki/Explicit_Congestion_Notification

Sascha
  • 101
  • 5
  • Thank you will surely try that. I was able to set the reserved bits in TCP header would it be okay if I use them for my application? – Hanzala Jamash Feb 13 '20 at 06:31
  • "Good thing about it is that you should be able to set the TOS (and thereby the ECN) on application level." I found resource on how to set the DSCP field but cant figure out how to set ECN at application level, can you provide any link regarding this – Hanzala Jamash Feb 13 '20 at 13:10