Thursday, May 10, 2018

pizza coming out!
  1. // Xilinx SDK includes
  2. #include "xparameters.h"
  3. #include
  4. #include
  5. #include "xtmrctr.h"
  6. #include "xio.h"
  7. #include
  8.  
  9. // WARP includes
  10. #include "wlan_mac_low.h"
  11. #include "w3_userio.h"
  12. #include "radio_controller.h"
  13.  
  14. #include "wlan_mac_ipc_util.h"
  15. #include "wlan_mac_802_11_defs.h"
  16. #include "wlan_mac_misc_util.h"
  17. #include "wlan_phy_util.h"
  18. #include "wlan_mac_jamming.h"
  19.  
  20. #include "wlan_exp.h"
  21. #include "math.h"
  22.  
  23.  
  24. /*************************** Constant Definitions ****************************/
  25.  
  26. #define WARPNET_TYPE_80211_LOW         WARPNET_TYPE_80211_LOW_JAMMING
  27. #define NUM_LEDS                       4
  28.  
  29. // Wifi channel, where the jammer should operate on
  30. #define WIFI_CHANNEL                   14
  31.  
  32. // Packet buffer used to hold the jamming frame
  33. #define TX_PKT_BUF_JAM                 7
  34.  
  35. /*********************** Global Variable Definitions *************************/
  36.  
  37.  
  38. /*************************** Variable Definitions ****************************/
  39. static u8                              eeprom_addr[6];
  40. static u8                              jamming;
  41. static u8                              showmacs;
  42.  
  43. volatile u8                            red_led_index;
  44. volatile u8                            green_led_index;
  45.  
  46. /**************************** Struct Definitions *****************************/
  47. typedef struct{
  48.     u8 dsap;
  49.     u8 ssap;
  50.     u8 control_field;
  51.     u8 org_code[3];
  52.     u16 type;
  53. } llc_header;
  54.  
  55. #define LLC_SNAP                        0xAA
  56. #define LLC_CNTRL_UNNUMBERED            0x03
  57.  
  58.  
  59. static u8                               OURMAC[6] = {0xDE,0xAD,0xBE,0xEF,0x01,0x01};
  60. static u8                               JAMMAC[6] = {0xDE,0xAD,0xBE,0xEF,0x11,0x11};
  61. /******************************** Functions **********************************/
  62.  
  63. inline void set_tx_power(u8 tx_pkt_buf, s8 power) {
  64.     tx_frame_info* mpdu_info = (tx_frame_info*) (TX_PKT_BUF_TO_ADDR(tx_pkt_buf));
  65.     mpdu_info->params.phy.power = power; // dBm
  66. }
  67.  
  68. inline void set_tx_ant_mode(u8 tx_pkt_buf, u8 antenna_mode) {
  69.     tx_frame_info* mpdu_info = (tx_frame_info*) (TX_PKT_BUF_TO_ADDR(tx_pkt_buf));
  70.     mpdu_info->params.phy.antenna_mode = antenna_mode;
  71. }
  72.  
  73. int main(){
  74.  
  75.     wlan_mac_hw_info* hw_info;
  76.     xil_printf("\f");
  77.     xil_printf("----- SEEMOO Reactive WiFi Jammer -------\n");
  78.     xil_printf("----- v0.1 ------------------------------\n");
  79.     xil_printf("----- wlan_mac_jamming ------------------\n");
  80.     xil_printf("Compiled %s %s\n\n", __DATE__, __TIME__);
  81.  
  82.     xil_printf("Note: this UART is currently printing from CPU_LOW. To view prints from\n");
  83.     xil_printf("and interact with CPU_HIGH, raise the right-most User I/O DIP switch bit.\n");
  84.     xil_printf("This switch can be toggled live while the design is running.\n\n");
  85.  
  86.     wlan_tx_config_ant_mode(TX_ANTMODE_SISO_ANTA);
  87.  
  88.     red_led_index = 0;
  89.     green_led_index = 0;
  90.     userio_write_leds_green(USERIO_BASEADDR, (1<<green_led_index));
  91.     userio_write_leds_red(USERIO_BASEADDR, (1<<red_led_index));
  92.  
  93.     wlan_mac_low_init(WARPNET_TYPE_80211_LOW);
  94.  
  95.     hw_info = wlan_mac_low_get_hw_info();
  96.     memcpy(eeprom_addr,hw_info->hw_addr_wlan,6);
  97.  
  98.  
  99.     wlan_mac_low_set_frame_rx_callback((void*)frame_receive);
  100.     wlan_mac_low_set_frame_tx_callback((void*)frame_transmit);
  101.  
  102.     wlan_mac_low_finish_init();
  103.  
  104.     REG_SET_BITS(WLAN_MAC_REG_CONTROL, (WLAN_MAC_CTRL_MASK_CCA_IGNORE_PHY_CS | WLAN_MAC_CTRL_MASK_CCA_IGNORE_NAV));
  105.  
  106.     // Set the WiFi channel
  107.     if(radio_controller_setCenterFrequency(RC_BASEADDR, (RC_ALL_RF), RC_24GHZ, wlan_mac_low_wlan_chan_to_rc_chan(WIFI_CHANNEL)) >= 0) {
  108.         xil_printf("WiFi Channel set to %d\n", WIFI_CHANNEL);
  109.     } else {
  110.         xil_printf("ERR: Setting the WiFi channel resulted in an error\n");
  111.     }
  112.  
  113.     xil_printf("Initialization Finished\n");
  114.  
  115.  
  116.  
  117.     while(1){
  118.  
  119.         //Poll PHY RX start
  120.         wlan_mac_low_poll_frame_rx();
  121.  
  122.         //Should check and handle push button events
  123.         //push_button_checker();
  124.     }
  125.     return 0;
  126. }
  127.  
  128.  
  129.  
  130.  
  131. /**
  132.  * @brief Checks which push button is pressed and calls functions to handle this event
  133.  */
  134. inline void push_button_checker() {
  135.  
  136.     if(userio_read_inputs(USERIO_BASEADDR) == W3_USERIO_PB_D){
  137.  
  138.  
  139.         //xil_printf("FRAME PREPARE");
  140.  
  141.             mac_header_80211* header = (mac_header_80211*)(TX_PKT_BUF_TO_ADDR(0)+PHY_TX_PKT_BUF_PHY_HDR_SIZE+sizeof(tx_frame_info));
  142.  
  143.             header->frame_control_1 = MAC_FRAME_CTRL1_SUBTYPE_DATA;
  144.             header->frame_control_2 = MAC_FRAME_CTRL2_FLAG_FROM_DS;
  145.             header->duration_id = 0;
  146.             header->sequence_control = 0;
  147.  
  148.             //header->address_1 = OURMAC;
  149.             memcpy(header->address_1, OURMAC, 6);
  150.             memcpy(header->address_2, OURMAC, 6);
  151.             memcpy(header->address_3, OURMAC, 6);
  152.  
  153.             set_tx_power(0,TX_POWER_MAX_DBM);
  154.             set_tx_ant_mode(0,TX_ANTMODE_SISO_ANTA);
  155.  
  156.             frame_transmit(0,WLAN_PHY_RATE_BPSK12,(u16) 1000,NULL);
  157.  
  158.             xil_printf("FRAME SENT");
  159.  
  160.             while(userio_read_inputs(USERIO_BASEADDR) == W3_USERIO_PB_D){
  161.                 //do nothing
  162.             }
  163.         }
  164.  
  165.     if(userio_read_inputs(USERIO_BASEADDR) == W3_USERIO_PB_U){
  166.         xil_printf(".");
  167.  
  168.         while(userio_read_inputs(USERIO_BASEADDR) == W3_USERIO_PB_U){
  169.             //do nothing
  170.         }
  171.     }
  172.  
  173.  
  174.  
  175.  
  176.  
  177. }
  178.  
  179. /**
  180.  * @brief Handles reception of a wireless packet
  181.  *
  182.  * This function is called after a good SIGNAL field is detected by either PHY (OFDM or DSSS)
  183.  * It is the responsibility of this function to wait until a sufficient number of bytes have been received
  184.  * before it can start to process those bytes. When this function is called the eventual checksum status is
  185.  * unknown. In NOMAC, this function doesn't need to do any kind of filtering or operations like transmitting
  186.  * an acknowledgment.
  187.  *
  188.  * @param u8 rx_pkt_buf
  189.  *  -Index of the Rx packet buffer containing the newly recevied packet
  190.  * @param u8 rate
  191.  *  -Index of PHY rate at which pcaket was received
  192.  * @param u16 length
  193.  *  -Number of bytes received by the PHY, including MAC header and FCS
  194.  * @return
  195.  *  - always returns 0 in NOMAC implementation
  196.  */
  197.  
  198.  
  199. u32 frame_receive(u8 rx_pkt_buf, u8 rate, u16 length){
  200.     //This function is called after a good SIGNAL field is detected by either PHY (OFDM or DSSS)
  201.     //It is the responsibility of this function to wait until a sufficient number of bytes have been received
  202.     // before it can start to process those bytes. When this function is called the eventual checksum status is
  203.     // unknown. The packet contents can be provisionally processed (e.g. prepare an ACK for fast transmission),
  204.     // but post-reception actions must be conditioned on the eventual FCS status (good or bad).
  205.     //
  206.     // Note: The timing of this function is critical for correct operation of the 802.11 DCF. It is not
  207.     // safe to add large delays to this function (e.g. xil_printf or usleep)
  208.  
  209.  
  210.     mac_header_80211* header = (mac_header_80211*)(RX_PKT_BUF_TO_ADDR(rx_pkt_buf)+PHY_RX_PKT_BUF_PHY_HDR_SIZE+sizeof(rx_frame_info));
  211.  
  212.     while(wlan_mac_get_last_byte()<=13+6){
  213.         //DO NOTHING
  214.     }
  215.  
  216.     if(wlan_addr_eq(header->address_1,JAMMAC)){
  217.         mac_header_80211* header = (mac_header_80211*)(TX_PKT_BUF_TO_ADDR(0)+PHY_TX_PKT_BUF_PHY_HDR_SIZE+sizeof(tx_frame_info));
  218.  
  219.         header->frame_control_1 = MAC_FRAME_CTRL1_SUBTYPE_DATA;
  220.         header->frame_control_2 = MAC_FRAME_CTRL2_FLAG_FROM_DS;
  221.         header->duration_id = 0;
  222.         header->sequence_control = 0;
  223.  
  224.         //header->address_1 = OURMAC;
  225.         memcpy(header->address_1, OURMAC, 6);
  226.         memcpy(header->address_2, OURMAC, 6);
  227.         memcpy(header->address_3, OURMAC, 6);
  228.  
  229.  
  230.         set_tx_power(0,TX_POWER_MAX_DBM);
  231.         set_tx_ant_mode(0,TX_ANTMODE_SISO_ANTA);
  232.  
  233.         frame_transmit(0,WLAN_PHY_RATE_BPSK12,(u16) 1000,NULL);
  234.  
  235.  
  236.         //xil_printf("Address: %x:%x:%x:%x:%x:%x \n",header->address_1[0],header->address_1[1],header->address_1[2],header->address_1[3],header->address_1[4],header->address_1[5]);
  237.     }
  238.  
  239.  
  240.  
  241.     u32 state = wlan_mac_dcf_hw_rx_finish(); //Blocks until reception is complete
  242.  
  243.     //xil_printf("Address: %x:%x:%x:%x:%x:%x \n",header->address_1[0],header->address_1[1],header->address_1[2],header->address_1[3],header->address_1[4],header->address_1[5]);
  244.  
  245.  
  246.  
  247.     if(state == RX_MPDU_STATE_FCS_GOOD){
  248.         green_led_index = (green_led_index + 1) % NUM_LEDS;
  249.         userio_write_leds_green(USERIO_BASEADDR, (1<<green_led_index));
  250.     } else {
  251.         red_led_index = (red_led_index + 1) % NUM_LEDS;
  252.         userio_write_leds_red(USERIO_BASEADDR, (1<<red_led_index));
  253.     }
  254.  
  255.  
  256.     // Clear packet buffer for debugging reasons
  257.     bzero((void *)(RX_PKT_BUF_TO_ADDR(rx_pkt_buf)), 2048);
  258.  
  259.  
  260.     // Set the OFDM and DSSS PHYs to use the same Rx pkt buffer
  261.     wlan_phy_rx_pkt_buf_ofdm(rx_pkt_buf);
  262.     wlan_phy_rx_pkt_buf_dsss(rx_pkt_buf);
  263.  
  264.     //Unblock the PHY post-Rx (no harm calling this if the PHY isn't actually blocked)
  265.     wlan_mac_dcf_hw_unblock_rx_phy();
  266.  
  267.  
  268.     return 0;
  269. }
  270.  
  271. /**
  272.  * @brief Handles transmission of a wireless packet
  273.  *
  274.  * This function is called to transmit a new packet via the PHY. While the code does utilize the wlan_mac_dcf_hw core,
  275.  * it bypasses any of the DCF-specific state in order to directly transmit the frame. This function should be called once per packet and will return
  276.  * immediately following that transmission. It will not perform any DCF-like retransmissions.
  277.  *
  278.  * This function is called once per IPC_MBOX_TX_MPDU_READY message from CPU High. The IPC_MBOX_TX_MPDU_DONE message will be sent
  279.  * back to CPU High when this function returns.
  280.  *
  281.  * @param u8 rx_pkt_buf
  282.  *  -Index of the Tx packet buffer containing the packet to transmit
  283.  * @param u8 rate
  284.  *  -Index of PHY rate at which packet will be transmitted
  285.  * @param u16 length
  286.  *  -Number of bytes in packet, including MAC header and FCS
  287.  * @param wlan_mac_low_tx_details* low_tx_details
  288.  *  -Pointer to array of metadata entries to be created for each PHY transmission of this packet (eventually leading to TX_LOW log entries)
  289.  * @return
  290.  *  -Transmission result
  291.  */
  292. int frame_transmit(u8 pkt_buf, u8 rate, u16 length, wlan_mac_low_tx_details* low_tx_details) {
  293.     //This function manages the MAC_DCF_HW core.
  294.  
  295.     u32 tx_status;
  296.     tx_frame_info* mpdu_info = (tx_frame_info*) (TX_PKT_BUF_TO_ADDR(pkt_buf));
  297.     u64 last_tx_timestamp;
  298.     int curr_tx_pow;
  299.     last_tx_timestamp = (u64)(mpdu_info->delay_accept) + (u64)(mpdu_info->timestamp_create);
  300.  
  301.     //Write the SIGNAL field (interpreted by the PHY during Tx waveform generation)
  302.     wlan_phy_set_tx_signal(pkt_buf, rate, length);
  303.  
  304.     unsigned char mpdu_tx_ant_mask = 0;
  305.     switch(mpdu_info->params.phy.antenna_mode) {
  306.         case TX_ANTMODE_SISO_ANTA:
  307.             mpdu_tx_ant_mask |= 0x1;
  308.         break;
  309.         case TX_ANTMODE_SISO_ANTB:
  310.             mpdu_tx_ant_mask |= 0x2;
  311.         break;
  312.         case TX_ANTMODE_SISO_ANTC:
  313.             mpdu_tx_ant_mask |= 0x4;
  314.         break;
  315.         case TX_ANTMODE_SISO_ANTD:
  316.             mpdu_tx_ant_mask |= 0x8;
  317.         break;
  318.         default:
  319.             mpdu_tx_ant_mask = 0x1;
  320.         break;
  321.     }
  322.  
  323.     mpdu_info->num_tx = 1;
  324.  
  325.     curr_tx_pow = wlan_mac_low_dbm_to_gain_target(mpdu_info->params.phy.power);
  326.  
  327.     wlan_mac_MPDU_tx_params(pkt_buf, 0, 0, mpdu_tx_ant_mask);
  328.  
  329.     //Set Tx Gains
  330.     wlan_mac_MPDU_tx_gains(curr_tx_pow,curr_tx_pow,curr_tx_pow,curr_tx_pow);
  331.  
  332.     //Before we mess with any PHY state, we need to make sure it isn't actively
  333.     //transmitting. For example, it may be sending an ACK when we get to this part of the code
  334.     while(wlan_mac_get_status() & WLAN_MAC_STATUS_MASK_PHY_TX_ACTIVE){}
  335.  
  336.     //Submit the MPDU for transmission - this starts the MAC hardware's MPDU Tx state machine
  337.     wlan_mac_MPDU_tx_start(1);
  338.     wlan_mac_MPDU_tx_start(0);
  339.  
  340.     //Wait for the MPDU Tx to finish
  341.     do{
  342.         if(low_tx_details != NULL){
  343.             low_tx_details[0].phy_params.rate = mpdu_info->params.phy.rate;
  344.             low_tx_details[0].phy_params.power = mpdu_info->params.phy.power;
  345.             low_tx_details[0].phy_params.antenna_mode = mpdu_info->params.phy.antenna_mode;
  346.             low_tx_details[0].chan_num = wlan_mac_low_get_active_channel();
  347.             low_tx_details[0].num_slots = 0;
  348.             low_tx_details[0].cw = 0;
  349.         }
  350.         tx_status = wlan_mac_get_status();
  351.  
  352.         if(tx_status & WLAN_MAC_STATUS_MASK_MPDU_TX_DONE) {
  353.             if(low_tx_details != NULL){
  354.                 low_tx_details[0].tx_start_delta = (u32)(get_tx_start_timestamp() - last_tx_timestamp);
  355.                 last_tx_timestamp = get_tx_start_timestamp();
  356.             }
  357.  
  358.             switch(tx_status & WLAN_MAC_STATUS_MASK_MPDU_TX_RESULT){
  359.                 case WLAN_MAC_STATUS_MPDU_TX_RESULT_SUCCESS:
  360.                     return 0;
  361.                 break;
  362.             }
  363.         }
  364.     } while(tx_status & WLAN_MAC_STATUS_MASK_MPDU_TX_PENDING);
  365.  
  366.     return -1;
  367. }

No comments: