common.c 15.1 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22 23
/*! \file common.c
* \brief implementation of emultor tx and rx
24
* \author Navid Nikaein, Lionel GAUTHIER, and Raymomd Knopp
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
* \date 2011
* \version 1.0
* \company Eurecom
* \email: navid.nikaein@eurecom.fr, lionel.gauthier@eurecom.fr
*/

#include "local.h"
#include "proto_extern.h"
#ifndef OAI_NW_DRIVER_USE_NETLINK
#include "rtai_fifos.h"
#endif


#include <linux/inetdevice.h>
#ifdef OAI_NW_DRIVER_TYPE_ETHERNET
#include <linux/etherdevice.h>
#endif

#include <net/tcp.h>
#include <net/udp.h>

#define NIPADDR(addr) \
        (uint8_t)(addr & 0x000000FF), \
        (uint8_t)((addr & 0x0000FF00) >> 8), \
        (uint8_t)((addr & 0x00FF0000) >> 16), \
        (uint8_t)((addr & 0xFF000000) >> 24)

#define NIP6ADDR(addr) \
        ntohs((addr)->s6_addr16[0]), \
        ntohs((addr)->s6_addr16[1]), \
        ntohs((addr)->s6_addr16[2]), \
        ntohs((addr)->s6_addr16[3]), \
        ntohs((addr)->s6_addr16[4]), \
        ntohs((addr)->s6_addr16[5]), \
        ntohs((addr)->s6_addr16[6]), \
        ntohs((addr)->s6_addr16[7])


63 64
//#define OAI_DRV_DEBUG_SEND
//#define OAI_DRV_DEBUG_RECEIVE
65
void oai_nw_drv_common_class_wireless2ip(uint16_t dlen,
66 67 68 69
    void *pdcp_sdu,
    int inst,
    OaiNwDrvRadioBearerId_t rb_id)
{
70 71

  //---------------------------------------------------------------------------
72 73 74 75 76 77 78 79 80 81 82 83 84
  struct sk_buff      *skb;
  struct ipversion    *ipv;
  struct oai_nw_drv_priv     *gpriv=netdev_priv(oai_nw_drv_dev[inst]);
  unsigned int         hard_header_len = 0;
  uint16_t                 *p_ether_type;
  uint16_t                  ether_type;
#ifdef OAI_DRV_DEBUG_RECEIVE
  int i;
  unsigned char *addr;
#endif
  unsigned char        protocol;
  struct iphdr        *network_header;

Lionel Gauthier's avatar
Lionel Gauthier committed
85 86 87 88 89
#ifdef NAS_ADDRESS_FIX
  uint32_t odaddr,osaddr;
  unsigned char *addr,*daddr,*saddr,*ifaddr;
  uint16_t *cksum,check;
#endif // NAS_ADDRESS_FIX
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[OAI_IP_DRV][%s] begin RB %d Inst %d Length %d bytes\n",__FUNCTION__, rb_id,inst,dlen);
#endif

  skb = dev_alloc_skb( dlen + 2 );

  if(!skb) {
    printk("[OAI_IP_DRV][%s] low on memory\n",__FUNCTION__);
    ++gpriv->stats.rx_dropped;
    return;
  }

  skb_reserve(skb,2);
  memcpy(skb_put(skb, dlen), pdcp_sdu,dlen);

  skb->dev = oai_nw_drv_dev[inst];
  hard_header_len = oai_nw_drv_dev[inst]->hard_header_len;

  skb_set_mac_header(skb, 0);
  skb_set_network_header(skb, hard_header_len);
  skb->mark = rb_id;
  skb->pkt_type = PACKET_HOST;


#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[OAI_IP_DRV][%s] Receiving packet @%p of size %d from PDCP \n",__FUNCTION__, skb->data, skb->len);

  for (i=0; i<skb->len; i++)
    printk("%2x ",((unsigned char *)(skb->data))[i]);

  printk("\n");
#endif



  // LG TEST skb->ip_summed = CHECKSUM_NONE;
  skb->ip_summed = CHECKSUM_UNNECESSARY;


  ipv = (struct ipversion *)skb_network_header(skb);

  switch (ipv->version) {

  case 6:
#ifdef OAI_DRV_DEBUG_RECEIVE
    printk("[OAI_IP_DRV][%s] receive IPv6 message\n",__FUNCTION__);
#endif
    skb_set_network_header(skb, hard_header_len);

    //skb->network_header = &skb->data[hard_header_len];
    if (hard_header_len == 0) {
      skb->protocol = htons(ETH_P_IPV6);
    } else {
#ifdef OAI_NW_DRIVER_TYPE_ETHERNET
      skb->protocol = eth_type_trans(skb, oai_nw_drv_dev[inst]);
#else
#endif
    }

    //printk("Writing packet with protocol %x\n",ntohs(skb->protocol));
    break;

  case 4:
#ifdef NAS_ADDRESS_FIX
    // Make the third byte of both the source and destination equal to the fourth of the destination
    daddr = (unsigned char *)&((struct iphdr *)&skb->data[hard_header_len])->daddr;
    odaddr = ((struct iphdr *)skb->data)->daddr;
    //sn = addr[3];
    saddr = (unsigned char *)&((struct iphdr *)&skb->data[hard_header_len])->saddr;
    osaddr = ((struct iphdr *)&skb->data[hard_header_len])->saddr;

    if (daddr[0] == saddr[0]) {// same network
      daddr[2] = daddr[3]; // set third byte of destination to that of local machine so that local IP stack accepts the packet
      saddr[2] = daddr[3]; // set third byte of source to that of local machine so that local IP stack accepts the packet
    }  else { // get the 3rd byte from device address in net_device structure
      ifaddr = (unsigned char *)(&(((struct in_device *)((oai_nw_drv_dev[inst])->ip_ptr))->ifa_list->ifa_local));

      if (saddr[0] == ifaddr[0]) { // source is in same network as local machine
        daddr[0] += saddr[3];        // fix address of remote destination to undo change at source
        saddr[2] =  ifaddr[2];       // set third byte to that of local machine so that local IP stack accepts the packet
      } else {                         // source is remote machine from outside network
        saddr[0] -= daddr[3];        // fix address of remote source to be understood by destination
        daddr[2] =  daddr[3];        // fix 3rd byte of local address to be understood by IP stack of
        // destination
      }
175 176
    }

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#endif //NAS_ADDRESS_FIX
#ifdef OAI_DRV_DEBUG_RECEIVE
    //printk("NAS_TOOL_RECEIVE: receive IPv4 message\n");
    addr = (unsigned char *)&((struct iphdr *)&skb->data[hard_header_len])->saddr;

    if (addr) {
      //addr[2]^=0x01;
      printk("[OAI_IP_DRV][%s] Source %d.%d.%d.%d\n",__FUNCTION__, addr[0],addr[1],addr[2],addr[3]);
    }

    addr = (unsigned char *)&((struct iphdr *)&skb->data[hard_header_len])->daddr;

    if (addr) {
      //addr[2]^=0x01;
      printk("[OAI_IP_DRV][%s] Dest %d.%d.%d.%d\n",__FUNCTION__, addr[0],addr[1],addr[2],addr[3]);
    }

    printk("[OAI_IP_DRV][%s] protocol  %d\n",__FUNCTION__, ((struct iphdr *)&skb->data[hard_header_len])->protocol);
#endif
196

197
    skb_set_network_header(skb, hard_header_len);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    //skb->network_header = &skb->data[hard_header_len];
    network_header = (struct iphdr *)skb_network_header(skb);
    protocol = network_header->protocol;

#ifdef OAI_DRV_DEBUG_RECEIVE

    switch (protocol) {
    case IPPROTO_IP:
      printk("[OAI_IP_DRV][%s] Received Raw IPv4 packet\n",__FUNCTION__);
      break;

    case IPPROTO_IPV6:
      printk("[OAI_IP_DRV][%s] Received Raw IPv6 packet\n",__FUNCTION__);
      break;

    case IPPROTO_ICMP:
      printk("[OAI_IP_DRV][%s] Received Raw ICMP packet\n",__FUNCTION__);
      break;

    case IPPROTO_TCP:
      printk("[OAI_IP_DRV][%s] Received TCP packet\n",__FUNCTION__);
      break;

    case IPPROTO_UDP:
      printk("[OAI_IP_DRV][%s] Received UDP packet\n",__FUNCTION__);
      break;

    default:
      break;
227
    }
228

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
#endif

#ifdef NAS_ADDRESS_FIX
    network_header->check = 0;
    network_header->check = ip_fast_csum((unsigned char *) network_header, network_header->ihl);
    //printk("[OAI_IP_DRV][COMMON][RECEIVE] IP Fast Checksum %x \n", network_header->check);

    switch(protocol) {
    case IPPROTO_TCP:

      cksum  = (uint16_t*)&(((struct tcphdr*)((network_header + (network_header->ihl<<2))))->check);
      check  = csum_tcpudp_magic(((struct iphdr *)network_header)->saddr, ((struct iphdr *)network_header)->daddr, 0,0, ~(*cksum));
      //check  = csum_tcpudp_magic(((struct iphdr *)network_header)->saddr, ((struct iphdr *)network_header)->daddr, tcp_hdrlen(skb), IPPROTO_TCP, ~(*cksum));
      //check  = csum_tcpudp_magic(((struct iphdr *)network_header)->saddr, ((struct iphdr *)network_header)->daddr, dlen, IPPROTO_TCP, ~(*cksum));

      *cksum = csum_tcpudp_magic(~osaddr, ~odaddr, 0, 0, ~check);
      //*cksum = csum_tcpudp_magic(~osaddr, ~odaddr, dlen, IPPROTO_TCP, ~check);
#ifdef OAI_DRV_DEBUG_RECEIVE
      printk("[OAI_IP_DRV][%s] Inst %d TCP packet calculated CS %x, CS = %x (before), SA (%x)%x, DA (%x)%x\n",__FUNCTION__,
             inst,
             network_header->check,
             *cksum,
             osaddr,
             ((struct iphdr *)skb->data)->saddr,
             odaddr,
             ((struct iphdr *)skb->data)->daddr);

      printk("[OAI_IP_DRV][%s] Inst %d TCP packet NEW CS %x\n",__FUNCTION__,
             inst,
             *cksum);
#endif
      break;

    case IPPROTO_UDP:
      cksum  = (uint16_t*)&(((struct udphdr*)((network_header + (network_header->ihl<<2))))->check);
      check = csum_tcpudp_magic(((struct iphdr *)network_header)->saddr, ((struct iphdr *)network_header)->daddr, 0,0, ~(*cksum));
      // check = csum_tcpudp_magic(((struct iphdr *)network_header)->saddr, ((struct iphdr *)network_header)->daddr, udp_hdr(skb)->len, IPPROTO_UDP, ~(*cksum));
      //check = csum_tcpudp_magic(((struct iphdr *)network_header)->saddr, ((struct iphdr *)network_header)->daddr, dlen, IPPROTO_UDP, ~(*cksum));

      *cksum= csum_tcpudp_magic(~osaddr, ~odaddr,0,0, ~check);
      //*cksum= csum_tcpudp_magic(~osaddr, ~odaddr,udp_hdr(skb)->len, IPPROTO_UDP, ~check);
      //*cksum= csum_tcpudp_magic(~osaddr, ~odaddr,dlen, IPPROTO_UDP, ~check);

#ifdef OAI_DRV_DEBUG_RECEIVE
      printk("[OAI_IP_DRV][%s] Inst %d UDP packet CS = %x (before), SA (%x)%x, DA (%x)%x\n",__FUNCTION__,
             inst,*cksum,osaddr,((struct iphdr *)&skb->data[hard_header_len])->saddr,odaddr,((struct iphdr *)&skb->data[hard_header_len])->daddr);

      printk("[OAI_IP_DRV][%s] Inst %d UDP packet NEW CS %x\n",__FUNCTION__,inst,*cksum);
#endif
      //if ((check = *cksum) != 0) {
      // src, dst, len, proto, sum
      //          }
      break;

    default:
      break;
    }

#endif //NAS_ADDRESS_FIX

    if (hard_header_len == 0) {
      skb->protocol = htons(ETH_P_IP);
    } else {
#ifdef OAI_NW_DRIVER_TYPE_ETHERNET
      skb->protocol = eth_type_trans(skb, oai_nw_drv_dev[inst]);
#else
#endif
    }

    //printk("[OAI_IP_DRV][COMMON] Writing packet with protocol %x\n",ntohs(skb->protocol));
    break;

  default:
    // fill skb->pkt_type, skb->dev

    skb->protocol = eth_type_trans(skb, oai_nw_drv_dev[inst]);
    // minus 1(short) instead of 2(bytes) because uint16_t*
    p_ether_type = (uint16_t *)(skb_network_header(skb)-2);
    ether_type = ntohs(*p_ether_type);

    switch (ether_type) {
    case ETH_P_ARP:
#ifdef OAI_DRV_DEBUG_RECEIVE
      printk("[OAI_IP_DRV][%s] ether_type = ETH_P_ARP\n",__FUNCTION__);
#endif
      //skb->pkt_type = PACKET_HOST;
      skb->protocol = htons(ETH_P_ARP);
      break;

    default:
      ;
    }

    printk("[OAI_IP_DRV][%s] begin RB %d Inst %d Length %d bytes\n",__FUNCTION__,rb_id,inst,dlen);
    printk("[OAI_IP_DRV][%s] Inst %d: receive unknown message (version=%d)\n",__FUNCTION__,inst,ipv->version);
  }

  ++gpriv->stats.rx_packets;
  gpriv->stats.rx_bytes += dlen;
#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[OAI_IP_DRV][%s] sending packet of size %d to kernel\n",__FUNCTION__,skb->len);

  for (i=0; i<skb->len; i++)
    printk("%2x ",((unsigned char *)(skb->data))[i]);

  printk("\n");
#endif //OAI_DRV_DEBUG_RECEIVE
336
  netif_rx_ni(skb);
337 338 339
#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[OAI_IP_DRV][%s] end\n",__FUNCTION__);
#endif
340 341 342 343
}

//---------------------------------------------------------------------------
// Delete the data
344 345
void oai_nw_drv_common_ip2wireless_drop(struct sk_buff *skb, int inst)
{
346 347 348 349 350 351 352
  //---------------------------------------------------------------------------
  struct oai_nw_drv_priv *priv=netdev_priv(oai_nw_drv_dev[inst]);
  ++priv->stats.tx_dropped;
}

//---------------------------------------------------------------------------
// Request the transfer of data (QoS SAP)
353 354
void oai_nw_drv_common_ip2wireless(struct sk_buff *skb, int inst)
{
355
  //---------------------------------------------------------------------------
356
  struct pdcp_data_req_header_s     pdcph;
357 358 359 360 361 362 363 364 365 366 367 368
  struct oai_nw_drv_priv *priv=netdev_priv(oai_nw_drv_dev[inst]);
#ifdef LOOPBACK_TEST
  int i;
#endif
#ifdef OAI_DRV_DEBUG_SEND
  int j;
#endif
  unsigned int bytes_wrote;
  // Start debug information
#ifdef OAI_DRV_DEBUG_SEND
  printk("[OAI_IP_DRV][%s] inst %d begin \n",__FUNCTION__,inst);
#endif
369

370
  if (skb==NULL) {
371 372 373 374 375
#ifdef OAI_DRV_DEBUG_SEND
    printk("[OAI_IP_DRV][%s] input parameter skb is NULL \n",__FUNCTION__);
#endif
    return;
  }
376

377 378 379 380 381
  pdcph.data_size    = skb->len;
  pdcph.rb_id        = skb->mark;
  pdcph.inst         = inst;
  pdcph.traffic_type = oai_nw_drv_find_traffic_type(skb);

382 383 384

  bytes_wrote = oai_nw_drv_netlink_send((char *)&pdcph,OAI_NW_DRV_PDCPH_SIZE);
#ifdef OAI_DRV_DEBUG_SEND
Lionel Gauthier's avatar
Lionel Gauthier committed
385
  printk("[OAI_IP_DRV][%s] Wrote %d bytes (header for %d byte skb) to PDCP RB %d via netlink\n",__FUNCTION__,
386
         bytes_wrote,skb->len, pdcph.rb_id);
387
#endif
388

389

390 391 392 393 394 395
  if (bytes_wrote != OAI_NW_DRV_PDCPH_SIZE) {
    printk("[OAI_IP_DRV][%s] problem while writing PDCP's header (bytes wrote = %d to fifo %d)\n",__FUNCTION__,bytes_wrote,IP2PDCP_FIFO);
    printk("rb_id %u, Wrote %u, Header Size %lu \n", pdcph.rb_id , bytes_wrote, OAI_NW_DRV_PDCPH_SIZE);
    priv->stats.tx_dropped ++;
    return;
  }
396 397

  bytes_wrote += oai_nw_drv_netlink_send((char *)skb->data,skb->len);
398

399

400 401 402 403 404 405 406 407 408 409 410 411
  if (bytes_wrote != skb->len+OAI_NW_DRV_PDCPH_SIZE) {
    printk("[OAI_IP_DRV][%s] Inst %d, RB_ID %u: problem while writing PDCP's data, bytes_wrote = %u, Data_len %u, PDCPH_SIZE %lu\n",
           __FUNCTION__,
           inst,
           pdcph.rb_id,
           bytes_wrote,
           skb->len,
           OAI_NW_DRV_PDCPH_SIZE); // congestion

    priv->stats.tx_dropped ++;
    return;
  }
412

413
#ifdef OAI_DRV_DEBUG_SEND
414
  printk("[OAI_IP_DRV][%s] Sending packet of size %d to PDCP traffic type %d\n",__FUNCTION__,skb->len, pdcph.traffic_type);
415

416
  for (j=0; j<skb->len; j++)
417
    printk("%2x ",((unsigned char *)(skb->data))[j]);
418

419 420 421 422 423 424 425 426 427 428 429 430
  printk("\n");
#endif

  priv->stats.tx_bytes   += skb->len;
  priv->stats.tx_packets ++;
#ifdef OAI_DRV_DEBUG_SEND
  printk("[OAI_IP_DRV][%s] end \n",__FUNCTION__);
#endif
}


//---------------------------------------------------------------------------
431 432 433
void oai_nw_drv_common_wireless2ip(struct nlmsghdr *nlh)
{
  //---------------------------------------------------------------------------
434

435
  struct pdcp_data_ind_header_s     *pdcph = (struct pdcp_data_ind_header_s *)NLMSG_DATA(nlh);
436 437 438 439 440 441 442 443 444
  struct oai_nw_drv_priv *priv;

  priv = netdev_priv(oai_nw_drv_dev[pdcph->inst]);

#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[OAI_IP_DRV][%s] QOS receive from PDCP, size %d, rab %d, inst %d\n",__FUNCTION__,
         pdcph->data_size,pdcph->rb_id,pdcph->inst);
#endif //OAI_DRV_DEBUG_RECEIVE

445
  oai_nw_drv_common_class_wireless2ip(pdcph->data_size,
446 447 448
                                      (unsigned char *)NLMSG_DATA(nlh) + OAI_NW_DRV_PDCPH_SIZE,
                                      pdcph->inst,
                                      pdcph->rb_id);
449 450 451
}