SlideShare une entreprise Scribd logo
1  sur  26
Chapter 16.


     ioctl Operations


              UNIX Network Programming
                                     1
16.1 Introduction
• The ioctl function has traditionally been the system interface
    used for everything that didn’t fit into some other nicely defined
    category.
•   Nevertheless numerous ioctls remain for implementation-
    dependent features related to network programming: obtaining
    the interface information, accessing the routing table, ARP cache
•   A common use of ioctl by network program ( typically server ) is
    to obtain information on all the host’s interfaces when the
    program starts : the interface addresses, whether the interface
    supports broadcasting, whether the interface supports
    multicasting, and so on.




                        UNIX Network Programming
                                               2
16.2 ioctl Function
#include <unistd.h>
int ioctl ( int fd, int request, . . . /* void *arg */ );
                                       Returns: 0 if OK, -1 on error

   – The third argument is always a pointer, but the type of pointer
     depends on the request.
   – We can divide the requests related to networking into sex
     categories.
      •   socket operations
      •   file operations
      •   interface operations
      •   ARP cache operations
      •   routing table operations
      •   streams system

                            UNIX Network Programming
                                                   3
UNIX Network Programming
                       4
16.3 Socket Operations
• SIOCATMASK
     • return through the integer pointed to by the third argument a nonzero value if
       the socket’s read pointer is currently at the out-of-band mark, of a zero value
       if the read pointer is not at the out-of-band mark.


• SIOCGPGRP
     • return through the integer pointed to by the third argument either the process
       ID or the process group ID that is set to receive the SIGIO or SIGURG signal
       for this socket.


• SIOCSPGRP
     • set either the process ID or the process group ID to receive the SIGIO or
       SIGURG signal for this socket from the integer pointed to by third argument.




                          UNIX Network Programming
                                                 5
16.4 File Operations
• FIONBIO
       • the nonblocking flag for the socket is cleared or turned on, depending whether
         the third argument to ioclt points to a zero or nonzero value, respectively.

• FIOASYNC
       • the flag that governs the receipt of asynchronous I/O signals(SIGIO) for the
         socket is cleared or turned on, depending whether the third argument to ioctl
         points a zero or nonzero value, respectively.

• FIONREAD
       • return in the integer pointed to by the third argument to ioctl the number of
         bytes currently in the socket receive buffer.
• FIOSETOWN - Equivalent to SIOCSPGRP for a socket
• FIOGETOWN - Equivalent to SIOCGPGRP for a socket


                            UNIX Network Programming
                                                   6
16.5 Interface Configuration
• From the kernel, get all the network interfaces configured on the
    system
•   In order to do it, use ioctl with SIOCGIFCONF
•   Before calling ioctl we allocate a buffer and an ifconf structure
    and the initialize the latter.
                     ifreq{}
                     ifc_len               1 0 2 4
                     ifc_req




                  F ig u re   1 6 .3   In it ia liz a t io n   o f i f c o n f s tru c tu re   b e fo re   SIOCGIFCONF.




                                  UNIX Network Programming
                                                         7
16.5 Interface Configuration
       ifreq{}
       ifc_len      64
       ifc_req
                                                        ifr_name[]

                                                                                ifreq{}
                                                              socket
                                                             ad d ress
                                                            stru ctu re


                                                        ifr_name[]

                                                                                ifreq{}
                                                              socket
                                                             ad d ress
                                                            stru ctu re




             F i g u r e 1 6 . 4 V a lu e s r e t u rn e d b y   SIOCGIFCONF.


                         UNIX Network Programming
                                                8
16.5 Interface Configuration
struct ifconf {
    int ifc_len;                        /* Size of buffer.   */
    union {
        caddr_t ifcu_buf;
        struct ifreq *ifcu_req;
      } ifc_ifcu;
  };
#define ifc_buf ifc_ifcu.ifcu_buf       /* Buffer address. */
#define ifc_req ifc_ifcu.ifcu_req       /* Array of structures.   */

#define IFNAMSIZ16
struct ifreq {
     char ifrn_name[IFNAMSIZ];        /* Interface name, e.g. "en0". */
     union {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        short int ifru_flags;
        int ifru_matric;
        caddr_t ifru_data;
      } ifr_ifru;
};
#define ifr_addr         ifr_ifru.ifru_addr         /* address                */
#define ifr_dstaddr     ifr_ifru.ifru_dstaddr       /* other end of p-p lnk   */
#define ifr_broadaddr   ifr_ifru.ifru_broadaddr      /* broadcast address     */
#define ifr_flags       ifr_ifru.ifru_flags         /* flags                  */
#define ifr_metric     ifr_ifru.ifru_ivalue         /* metric                 */
#define ifr_data        ifr_ifru.ifru_data          /* for use by interface   */

                           UNIX Network Programming
                                                  9
16.6 get_cli_info function
• Since many programs need to know all the interfaces on a
  system, we will develop a function of our own named
  get_ifi_info that returns a linked list of structures, one for each
  interface that is currently “up”.




                       UNIX Network Programming
                                              10
Figure 16.5 unpifi.h header.
#include "unp.h"
#include <net/if.h>

#define    IFI_NAME               16         /* same as IFNAMSIZ in <net/if.h> */
#define    IFI_HADDR               8         /* allow for 64-bit EUI-64 in future */

struct ifi_info {
 char ifi_name[IFI_NAME];                       /* interface name, null terminated */
 u_char ifi_haddr[IFI_HADDR];                   /* hardware address */
 u_short ifi_hlen;                              /* #bytes in hardware address: 0, 6, 8 */
 short ifi_flags;                               /* IFF_xxx constants from <net/if.h> */
 short ifi_myflags;                             /* our own IFI_xxx flags */
 struct sockaddr *ifi_addr;                     /* primary address */
 struct sockaddr *ifi_brdaddr;/* broadcast address */
 struct sockaddr *ifi_dstaddr;                  /* destination address */
 struct ifi_info *ifi_next;                     /* next of these structures */
};
#define IFI_ALIAS                   1           /* ifi_addr is an alias */
                        /* function prototypes */
struct ifi_info         *get_ifi_info(int, int);
struct ifi_info         *Get_ifi_info(int, int);
void         free_ifi_info(struct ifi_info *);

                              UNIX Network Programming
                                                     11
Figure 16.6 prifinfo program that calls our get_ifi_info function. (1/3)

#include "unpifi.h"

int
main(int argc, char **argv)
{
          struct ifi_info       *ifi, *ifihead;
          struct sockaddr       *sa;
          u_char                *ptr;
          int                   i, family, doaliases;

          if (argc != 3)
                      err_quit("usage: prifinfo <inet4|inet6> <doaliases>");

          if (strcmp(argv[1], "inet4") == 0)
                       family = AF_INET;
#ifdef    IPV6
          else if (strcmp(argv[1], "inet6") == 0)
                       family = AF_INET6;
#endif
          else
                     err_quit("invalid <address-family>");
          doaliases = atoi(argv[2]);

                                 UNIX Network Programming
                                                        12
Figure 16.6 prifinfo program that calls our get_ifi_info function. (2/3)




    for (ifihead = ifi = Get_ifi_info(family, doaliases);
               ifi != NULL; ifi = ifi->ifi_next) {
              printf("%s: <", ifi->ifi_name);


              if (ifi->ifi_flags & IFF_UP)                  printf("UP ");
              if (ifi->ifi_flags & IFF_BROADCAST)           printf("BCAST ");
              if (ifi->ifi_flags & IFF_MULTICAST)           printf("MCAST ");
              if (ifi->ifi_flags & IFF_LOOPBACK)            printf("LOOP ");
              if (ifi->ifi_flags & IFF_POINTOPOINT)         printf("P2P ");
              printf(">n");




                               UNIX Network Programming
                                                      13
Figure 16.6 prifinfo program that calls our get_ifi_info function. (3/3)

                    if ( (i = ifi->ifi_hlen) > 0) {
                                  ptr = ifi->ifi_haddr;
                                  do {
                                              printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++);
                                  } while (--i > 0);
                                  printf("n");
                    }

                    if ( (sa = ifi->ifi_addr) != NULL)
                                  printf(" IP addr: %sn",
                                                                 Sock_ntop_host(sa, sizeof(*sa)));
                    if ( (sa = ifi->ifi_brdaddr) != NULL)
                                  printf(" broadcast addr: %sn",
                                                                 Sock_ntop_host(sa, sizeof(*sa)));
                    if ( (sa = ifi->ifi_dstaddr) != NULL)
                                  printf(" destination addr: %sn",
                                                                 Sock_ntop_host(sa, sizeof(*sa)));
         }
         free_ifi_info(ifihead);
         exit(0);
}

                                    UNIX Network Programming
                                                           14
lib/get_ifi_info.c (1/6)

#include "unpifi.h"

struct ifi_info *
get_ifi_info(int family, int doaliases)
{
           struct ifi_info                *ifi, *ifihead, **ifipnext;
           int                            sockfd, len, lastlen, flags, myflags;
           char                           *ptr, *buf, lastname[IFNAMSIZ], *cptr;
           struct ifconf                  ifc;
           struct ifreq                   *ifr, ifrcopy;
           struct sockaddr_in *sinptr;

          sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

          lastlen = 0;
          len = 100 * sizeof(struct ifreq);        /* initial buffer size guess */




                                UNIX Network Programming
                                                       15
lib/get_ifi_info.c (2/6)


       for ( ; ; ) {
                    buf = Malloc(len);
                    ifc.ifc_len = len;
                    ifc.ifc_buf = buf;
                    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
                                if (errno != EINVAL || lastlen != 0)
                                            err_sys("ioctl error");
                    } else {
                                if (ifc.ifc_len == lastlen)
                                            break;               /* success, len has not changed */
                                lastlen = ifc.ifc_len;
                    }
                    len += 10 * sizeof(struct ifreq);            /* increment */
                    free(buf);
       }
       ifihead = NULL;
       ifipnext = &ifihead;
       lastname[0] = 0;


                                 UNIX Network Programming
                                                        16
lib/get_ifi_info.c (3/6)

           for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
                      ifr = (struct ifreq *) ptr;

 #ifdef    HAVE_SOCKADDR_SA_LEN
                 len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
 #else
                     switch (ifr->ifr_addr.sa_family) {
 #ifdef    IPV6
                     case AF_INET6:
                              len = sizeof(struct sockaddr_in6);
                              break;
 #endif
                  case AF_INET:
                  default:
                            len = sizeof(struct sockaddr);
                            break;
                  }
 #endif    /* HAVE_SOCKADDR_SA_LEN */
                  ptr += sizeof(ifr->ifr_name) + len;      /* for next one in buffer */


                                UNIX Network Programming
                                                       17
lib/get_ifi_info.c (4/6)


                 if (ifr->ifr_addr.sa_family != family)
                             continue; /* ignore if not desired address family */

                 myflags = 0;
                 if ( (cptr = strchr(ifr->ifr_name, ':')) != NULL)
                             *cptr = 0;           /* replace colon will null */
                 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
                             if (doaliases == 0)
                                        continue; /* already processed this interface */
                             myflags = IFI_ALIAS;
                 }
                 memcpy(lastname, ifr->ifr_name, IFNAMSIZ);

                 ifrcopy = *ifr;
                 Ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
                 flags = ifrcopy.ifr_flags;
                 if ((flags & IFF_UP) == 0)
                            continue; /* ignore if interface not up */


                                UNIX Network Programming
                                                       18
lib/get_ifi_info.c (5/6)


            ifi = Calloc(1, sizeof(struct ifi_info));
            *ifipnext = ifi;                            /* prev points to this new one */
            ifipnext = &ifi->ifi_next;       /* pointer to next one goes here */

            ifi->ifi_flags = flags;               /* IFF_xxx values */
            ifi->ifi_myflags = myflags; /* IFI_xxx values */
            memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
            ifi->ifi_name[IFI_NAME-1] = '0';

            switch (ifr->ifr_addr.sa_family) {
            case AF_INET:
                       sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
                       if (ifi->ifi_addr == NULL) {
                                   ifi->ifi_addr = Calloc(1, sizeof(struct sockaddr_in));
                                   memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));




                                UNIX Network Programming
                                                       19
lib/get_ifi_info.c (6/6)
#ifdef     SIOCGIFBRDADDR
                         if (flags & IFF_BROADCAST) {
                                     Ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy);
                                     sinptr = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr;
                                     ifi->ifi_brdaddr = Calloc(1, sizeof(struct sockaddr_in));
                                     memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in));
                         }
#endif

#ifdef     SIOCGIFDSTADDR
                         if (flags & IFF_POINTOPOINT) {
                                     Ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy);
                                     sinptr = (struct sockaddr_in *) &ifrcopy.ifr_dstaddr;
                                     ifi->ifi_dstaddr = Calloc(1, sizeof(struct sockaddr_in));
                                     memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in));
                         }
#endif
                      }
                      break;

           default:
                      break;
           }
     }
     free(buf);
    return(ifihead); /* pointer to first structure in linked list */
}                                   UNIX Network Programming
                                                           20
Figure 16.10 free_ifi_info function: free dynamic memory allocated by
get _ifi_info.


void
free_ifi_info(struct ifi_info *ifihead)
{
           struct ifi_info      *ifi, *ifinext;

          for (ifi = ifihead; ifi != NULL; ifi = ifinext) {
                      if (ifi->ifi_addr != NULL)
                                  free(ifi->ifi_addr);
                      if (ifi->ifi_brdaddr != NULL)
                                  free(ifi->ifi_brdaddr);
                      if (ifi->ifi_dstaddr != NULL)
                                  free(ifi->ifi_dstaddr);
                      ifinext = ifi->ifi_next;         /* can't fetch ifi_next after free() */
                      free(ifi);                       /* the ifi_info{} itself */
          }
}


                                   UNIX Network Programming
                                                          21
16.7 Interface Operations
• SIOCGIFADDR
     – return the unicast address in the ifr_addr member.
•   SIOCSIFADDR
     – sets the interface address from the ifr_addr member.
•   SIOCGIFFLAGS
     – return the interface flags in the ifr_flags member.
•   SIOCSIFFLAGS
     – sets the interface flags from the ifr_flags member.
•   SIOCGIFDSTADDR
     – return the point-to-point address in the ifr_dstaddr member.
•   SIOCSIFDSTADDR
     – set the point-to-point address from ifr_dstaddr member.
                        UNIX Network Programming
                                               22
16.7 Interface Operations
• SIOCGIFBRDADDR
     – return the broadcast address in the ifr_broadaddr emember.
•   SIOCSIFBRDADDR
     – set the broadcast address from the ifr_boradaddr member.
•   SIOCGIFNETMASK
     – return the subnet mask in the ifr_addr member.
•   SIOCSIFNETMASK
     – set the subnet mask from the ifr_addr member.
•   SIOCGIFMETRIC
     – return the interface metric in the ifr_metric member.
•   SIOCSIFMETRIC
     – set the interface routing metric from the ifr_metric member.
                        UNIX Network Programming
                                               23
16.8 ARP Cache Operations
struct arpreq {
   struct   sockaddr         arp_pa;        /* protocol addesss */
   struct   sockaddr         arp_ha;        /* hardware address */
   int                       arp_flags;     /* flags */
};

#define   ATF_INUSE   0x01   /*   entry in use */
#define   ATF_COM     0x02   /*   completed entry (hardware addr valid) */
#define   ATF_PERM    0x04   /*   permanent entry */
#define   ATF_PUBL    0x08   /*   published entry (respond for other host) */

• SIOCSARP
     – add a new entry to the ARP cache or modify an existing
       entry.
•   SIOCDARP
     – delete an entry from the ARP cache.
•   SIOCGARP
     – get an entry from the ARP cache.
                         UNIX Network Programming 24
Figure 16.12 Print a host’s hardware addresses.
int main(int argc, char **argv)
{
            int                         family, sockfd;
            char                        str[INET6_ADDRSTRLEN];
            char                        **pptr;
            unsigned char               *ptr;
            struct arpreq                           arpreq;
            struct sockaddr_in          *sin;

            pptr = my_addrs(&family);
            for ( ; *pptr != NULL; pptr++) {
                         printf("%s: ", Inet_ntop(family, *pptr, str, sizeof(str)));
                         switch (family) {
                         case AF_INET:
                                     sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
                                     sin = (struct sockaddr_in *) &arpreq.arp_pa;
                                     bzero(sin, sizeof(struct sockaddr_in));
                                     sin->sin_family = AF_INET;
                                     memcpy(&sin->sin_addr, *pptr, sizeof(struct in_addr));
                                     Ioctl(sockfd, SIOCGARP, &arpreq);
                                     ptr = &arpreq.arp_ha.sa_data[0];
                                     printf("%x:%x:%x:%x:%x:%xn", *ptr, *(ptr+1),
                                                    *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));
                                     break;

                             default:
                                        err_quit("unsupported address family: %d", family);
            } } exit(0); }               UNIX Network Programming
                                                                25
16.9 Routing Table Operations
• Two ioctl requests are provided to operate on the routing table.
• These two requests require that the third argument to ioctl be a
    pointer to an rtentry structure,which is defined by including the
    <net/route.h> header.
•   Only the superuser can issue these requests.

• SIOCADDRT - add an entry to the routing table.
• SIOCDELRT - delete an entry to the routing table.




                        UNIX Network Programming
                                               26

Contenu connexe

Tendances

CEFv6 in a nutshell
CEFv6 in a nutshellCEFv6 in a nutshell
CEFv6 in a nutshellFred Bovy
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR mattersAlexandre Moneger
 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDBJian-Yu Li
 
Cisco IOS shellcode: All-in-one
Cisco IOS shellcode: All-in-oneCisco IOS shellcode: All-in-one
Cisco IOS shellcode: All-in-oneDefconRussia
 
Unit 2
Unit 2Unit 2
Unit 2siddr
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programmingelliando dias
 
OSPF (open shortest path first) part iii
OSPF (open shortest path first) part  iiiOSPF (open shortest path first) part  iii
OSPF (open shortest path first) part iiiNetwax Lab
 
20190521 pwn 101_by_roy
20190521 pwn 101_by_roy20190521 pwn 101_by_roy
20190521 pwn 101_by_royRoy
 
OSPF Route Filtering
OSPF Route FilteringOSPF Route Filtering
OSPF Route FilteringNetwax Lab
 
OSPF (open shortest path first) part ii
OSPF (open shortest path first) part  iiOSPF (open shortest path first) part  ii
OSPF (open shortest path first) part iiNetwax Lab
 
Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...
Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...
Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...idsecconf
 
penetration testing - black box type.
penetration testing - black box type.penetration testing - black box type.
penetration testing - black box type.luigi capuzzello
 
Juniper JNCIA – Juniper OSPF Route Configuration
Juniper JNCIA – Juniper OSPF Route ConfigurationJuniper JNCIA – Juniper OSPF Route Configuration
Juniper JNCIA – Juniper OSPF Route ConfigurationHamed Moghaddam
 
리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2Sangho Park
 

Tendances (20)

CEFv6 in a nutshell
CEFv6 in a nutshellCEFv6 in a nutshell
CEFv6 in a nutshell
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters
 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDB
 
Cisco IOS shellcode: All-in-one
Cisco IOS shellcode: All-in-oneCisco IOS shellcode: All-in-one
Cisco IOS shellcode: All-in-one
 
Unit 2
Unit 2Unit 2
Unit 2
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
 
Select and poll functions
Select and poll functionsSelect and poll functions
Select and poll functions
 
iCloud keychain
iCloud keychainiCloud keychain
iCloud keychain
 
OSPF (open shortest path first) part iii
OSPF (open shortest path first) part  iiiOSPF (open shortest path first) part  iii
OSPF (open shortest path first) part iii
 
Linux
LinuxLinux
Linux
 
20190521 pwn 101_by_roy
20190521 pwn 101_by_roy20190521 pwn 101_by_roy
20190521 pwn 101_by_roy
 
Useful Linux commands
Useful Linux commandsUseful Linux commands
Useful Linux commands
 
OSPF Route Filtering
OSPF Route FilteringOSPF Route Filtering
OSPF Route Filtering
 
OSPF (open shortest path first) part ii
OSPF (open shortest path first) part  iiOSPF (open shortest path first) part  ii
OSPF (open shortest path first) part ii
 
Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...
Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...
Information Theft: Wireless Router Shareport for Phun and profit - Hero Suhar...
 
penetration testing - black box type.
penetration testing - black box type.penetration testing - black box type.
penetration testing - black box type.
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Juniper JNCIA – Juniper OSPF Route Configuration
Juniper JNCIA – Juniper OSPF Route ConfigurationJuniper JNCIA – Juniper OSPF Route Configuration
Juniper JNCIA – Juniper OSPF Route Configuration
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2
 

Similaire à Npc16

Network security Lab manual
Network security Lab manual Network security Lab manual
Network security Lab manual Vivek Kumar Sinha
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelDivye Kapoor
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernelKiran Divekar
 
Introduction to Kernel Programming
Introduction to Kernel ProgrammingIntroduction to Kernel Programming
Introduction to Kernel ProgrammingAhmed Mekkawy
 
Dynamische Routingprotokolle Aufzucht und Pflege - OSPF
Dynamische Routingprotokolle Aufzucht und Pflege - OSPFDynamische Routingprotokolle Aufzucht und Pflege - OSPF
Dynamische Routingprotokolle Aufzucht und Pflege - OSPFMaximilan Wilhelm
 
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
[2007 CodeEngn Conference 01] seaofglass - Linux Virus AnalysisGangSeok Lee
 
07 - Bypassing ASLR, or why X^W matters
07 - Bypassing ASLR, or why X^W matters07 - Bypassing ASLR, or why X^W matters
07 - Bypassing ASLR, or why X^W mattersAlexandre Moneger
 
InfiniFlux IP Address Type
InfiniFlux IP Address TypeInfiniFlux IP Address Type
InfiniFlux IP Address TypeInfiniFlux
 

Similaire à Npc16 (20)

Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Network security Lab manual
Network security Lab manual Network security Lab manual
Network security Lab manual
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
 
Sockets and Socket-Buffer
Sockets and Socket-BufferSockets and Socket-Buffer
Sockets and Socket-Buffer
 
Sysprog 16
Sysprog 16Sysprog 16
Sysprog 16
 
Introduction to Kernel Programming
Introduction to Kernel ProgrammingIntroduction to Kernel Programming
Introduction to Kernel Programming
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Computer networkppt4577
Computer networkppt4577Computer networkppt4577
Computer networkppt4577
 
sockets
socketssockets
sockets
 
Dynamische Routingprotokolle Aufzucht und Pflege - OSPF
Dynamische Routingprotokolle Aufzucht und Pflege - OSPFDynamische Routingprotokolle Aufzucht und Pflege - OSPF
Dynamische Routingprotokolle Aufzucht und Pflege - OSPF
 
Sockets
SocketsSockets
Sockets
 
Socket programming
Socket programming Socket programming
Socket programming
 
Npc14
Npc14Npc14
Npc14
 
Network sockets
Network socketsNetwork sockets
Network sockets
 
Embedded C - Lecture 4
Embedded C - Lecture 4Embedded C - Lecture 4
Embedded C - Lecture 4
 
Npc13
Npc13Npc13
Npc13
 
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
[2007 CodeEngn Conference 01] seaofglass - Linux Virus Analysis
 
07 - Bypassing ASLR, or why X^W matters
07 - Bypassing ASLR, or why X^W matters07 - Bypassing ASLR, or why X^W matters
07 - Bypassing ASLR, or why X^W matters
 
InfiniFlux IP Address Type
InfiniFlux IP Address TypeInfiniFlux IP Address Type
InfiniFlux IP Address Type
 

Plus de vamsitricks (20)

Unit 6
Unit 6Unit 6
Unit 6
 
Np unit2
Np unit2Np unit2
Np unit2
 
Np unit1
Np unit1Np unit1
Np unit1
 
Np unit iv i
Np unit iv iNp unit iv i
Np unit iv i
 
Np unit iii
Np unit iiiNp unit iii
Np unit iii
 
Np unit iv ii
Np unit iv iiNp unit iv ii
Np unit iv ii
 
Unit 7
Unit 7Unit 7
Unit 7
 
Npc08
Npc08Npc08
Npc08
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 7
Unit 7Unit 7
Unit 7
 
Unit 6
Unit 6Unit 6
Unit 6
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Unit3wt
Unit3wtUnit3wt
Unit3wt
 
Unit2wt
Unit2wtUnit2wt
Unit2wt
 
Unit 1wt
Unit 1wtUnit 1wt
Unit 1wt
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 

Npc16

  • 1. Chapter 16. ioctl Operations UNIX Network Programming 1
  • 2. 16.1 Introduction • The ioctl function has traditionally been the system interface used for everything that didn’t fit into some other nicely defined category. • Nevertheless numerous ioctls remain for implementation- dependent features related to network programming: obtaining the interface information, accessing the routing table, ARP cache • A common use of ioctl by network program ( typically server ) is to obtain information on all the host’s interfaces when the program starts : the interface addresses, whether the interface supports broadcasting, whether the interface supports multicasting, and so on. UNIX Network Programming 2
  • 3. 16.2 ioctl Function #include <unistd.h> int ioctl ( int fd, int request, . . . /* void *arg */ ); Returns: 0 if OK, -1 on error – The third argument is always a pointer, but the type of pointer depends on the request. – We can divide the requests related to networking into sex categories. • socket operations • file operations • interface operations • ARP cache operations • routing table operations • streams system UNIX Network Programming 3
  • 5. 16.3 Socket Operations • SIOCATMASK • return through the integer pointed to by the third argument a nonzero value if the socket’s read pointer is currently at the out-of-band mark, of a zero value if the read pointer is not at the out-of-band mark. • SIOCGPGRP • return through the integer pointed to by the third argument either the process ID or the process group ID that is set to receive the SIGIO or SIGURG signal for this socket. • SIOCSPGRP • set either the process ID or the process group ID to receive the SIGIO or SIGURG signal for this socket from the integer pointed to by third argument. UNIX Network Programming 5
  • 6. 16.4 File Operations • FIONBIO • the nonblocking flag for the socket is cleared or turned on, depending whether the third argument to ioclt points to a zero or nonzero value, respectively. • FIOASYNC • the flag that governs the receipt of asynchronous I/O signals(SIGIO) for the socket is cleared or turned on, depending whether the third argument to ioctl points a zero or nonzero value, respectively. • FIONREAD • return in the integer pointed to by the third argument to ioctl the number of bytes currently in the socket receive buffer. • FIOSETOWN - Equivalent to SIOCSPGRP for a socket • FIOGETOWN - Equivalent to SIOCGPGRP for a socket UNIX Network Programming 6
  • 7. 16.5 Interface Configuration • From the kernel, get all the network interfaces configured on the system • In order to do it, use ioctl with SIOCGIFCONF • Before calling ioctl we allocate a buffer and an ifconf structure and the initialize the latter. ifreq{} ifc_len 1 0 2 4 ifc_req F ig u re 1 6 .3 In it ia liz a t io n o f i f c o n f s tru c tu re b e fo re SIOCGIFCONF. UNIX Network Programming 7
  • 8. 16.5 Interface Configuration ifreq{} ifc_len 64 ifc_req ifr_name[] ifreq{} socket ad d ress stru ctu re ifr_name[] ifreq{} socket ad d ress stru ctu re F i g u r e 1 6 . 4 V a lu e s r e t u rn e d b y SIOCGIFCONF. UNIX Network Programming 8
  • 9. 16.5 Interface Configuration struct ifconf { int ifc_len; /* Size of buffer. */ union { caddr_t ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }; #define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address. */ #define ifc_req ifc_ifcu.ifcu_req /* Array of structures. */ #define IFNAMSIZ16 struct ifreq { char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */ union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; short int ifru_flags; int ifru_matric; caddr_t ifru_data; } ifr_ifru; }; #define ifr_addr ifr_ifru.ifru_addr /* address */ #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */ #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ #define ifr_flags ifr_ifru.ifru_flags /* flags */ #define ifr_metric ifr_ifru.ifru_ivalue /* metric */ #define ifr_data ifr_ifru.ifru_data /* for use by interface */ UNIX Network Programming 9
  • 10. 16.6 get_cli_info function • Since many programs need to know all the interfaces on a system, we will develop a function of our own named get_ifi_info that returns a linked list of structures, one for each interface that is currently “up”. UNIX Network Programming 10
  • 11. Figure 16.5 unpifi.h header. #include "unp.h" #include <net/if.h> #define IFI_NAME 16 /* same as IFNAMSIZ in <net/if.h> */ #define IFI_HADDR 8 /* allow for 64-bit EUI-64 in future */ struct ifi_info { char ifi_name[IFI_NAME]; /* interface name, null terminated */ u_char ifi_haddr[IFI_HADDR]; /* hardware address */ u_short ifi_hlen; /* #bytes in hardware address: 0, 6, 8 */ short ifi_flags; /* IFF_xxx constants from <net/if.h> */ short ifi_myflags; /* our own IFI_xxx flags */ struct sockaddr *ifi_addr; /* primary address */ struct sockaddr *ifi_brdaddr;/* broadcast address */ struct sockaddr *ifi_dstaddr; /* destination address */ struct ifi_info *ifi_next; /* next of these structures */ }; #define IFI_ALIAS 1 /* ifi_addr is an alias */ /* function prototypes */ struct ifi_info *get_ifi_info(int, int); struct ifi_info *Get_ifi_info(int, int); void free_ifi_info(struct ifi_info *); UNIX Network Programming 11
  • 12. Figure 16.6 prifinfo program that calls our get_ifi_info function. (1/3) #include "unpifi.h" int main(int argc, char **argv) { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; u_char *ptr; int i, family, doaliases; if (argc != 3) err_quit("usage: prifinfo <inet4|inet6> <doaliases>"); if (strcmp(argv[1], "inet4") == 0) family = AF_INET; #ifdef IPV6 else if (strcmp(argv[1], "inet6") == 0) family = AF_INET6; #endif else err_quit("invalid <address-family>"); doaliases = atoi(argv[2]); UNIX Network Programming 12
  • 13. Figure 16.6 prifinfo program that calls our get_ifi_info function. (2/3) for (ifihead = ifi = Get_ifi_info(family, doaliases); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: <", ifi->ifi_name); if (ifi->ifi_flags & IFF_UP) printf("UP "); if (ifi->ifi_flags & IFF_BROADCAST) printf("BCAST "); if (ifi->ifi_flags & IFF_MULTICAST) printf("MCAST "); if (ifi->ifi_flags & IFF_LOOPBACK) printf("LOOP "); if (ifi->ifi_flags & IFF_POINTOPOINT) printf("P2P "); printf(">n"); UNIX Network Programming 13
  • 14. Figure 16.6 prifinfo program that calls our get_ifi_info function. (3/3) if ( (i = ifi->ifi_hlen) > 0) { ptr = ifi->ifi_haddr; do { printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++); } while (--i > 0); printf("n"); } if ( (sa = ifi->ifi_addr) != NULL) printf(" IP addr: %sn", Sock_ntop_host(sa, sizeof(*sa))); if ( (sa = ifi->ifi_brdaddr) != NULL) printf(" broadcast addr: %sn", Sock_ntop_host(sa, sizeof(*sa))); if ( (sa = ifi->ifi_dstaddr) != NULL) printf(" destination addr: %sn", Sock_ntop_host(sa, sizeof(*sa))); } free_ifi_info(ifihead); exit(0); } UNIX Network Programming 14
  • 15. lib/get_ifi_info.c (1/6) #include "unpifi.h" struct ifi_info * get_ifi_info(int family, int doaliases) { struct ifi_info *ifi, *ifihead, **ifipnext; int sockfd, len, lastlen, flags, myflags; char *ptr, *buf, lastname[IFNAMSIZ], *cptr; struct ifconf ifc; struct ifreq *ifr, ifrcopy; struct sockaddr_in *sinptr; sockfd = Socket(AF_INET, SOCK_DGRAM, 0); lastlen = 0; len = 100 * sizeof(struct ifreq); /* initial buffer size guess */ UNIX Network Programming 15
  • 16. lib/get_ifi_info.c (2/6) for ( ; ; ) { buf = Malloc(len); ifc.ifc_len = len; ifc.ifc_buf = buf; if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { if (errno != EINVAL || lastlen != 0) err_sys("ioctl error"); } else { if (ifc.ifc_len == lastlen) break; /* success, len has not changed */ lastlen = ifc.ifc_len; } len += 10 * sizeof(struct ifreq); /* increment */ free(buf); } ifihead = NULL; ifipnext = &ifihead; lastname[0] = 0; UNIX Network Programming 16
  • 17. lib/get_ifi_info.c (3/6) for (ptr = buf; ptr < buf + ifc.ifc_len; ) { ifr = (struct ifreq *) ptr; #ifdef HAVE_SOCKADDR_SA_LEN len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len); #else switch (ifr->ifr_addr.sa_family) { #ifdef IPV6 case AF_INET6: len = sizeof(struct sockaddr_in6); break; #endif case AF_INET: default: len = sizeof(struct sockaddr); break; } #endif /* HAVE_SOCKADDR_SA_LEN */ ptr += sizeof(ifr->ifr_name) + len; /* for next one in buffer */ UNIX Network Programming 17
  • 18. lib/get_ifi_info.c (4/6) if (ifr->ifr_addr.sa_family != family) continue; /* ignore if not desired address family */ myflags = 0; if ( (cptr = strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0; /* replace colon will null */ if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) { if (doaliases == 0) continue; /* already processed this interface */ myflags = IFI_ALIAS; } memcpy(lastname, ifr->ifr_name, IFNAMSIZ); ifrcopy = *ifr; Ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy); flags = ifrcopy.ifr_flags; if ((flags & IFF_UP) == 0) continue; /* ignore if interface not up */ UNIX Network Programming 18
  • 19. lib/get_ifi_info.c (5/6) ifi = Calloc(1, sizeof(struct ifi_info)); *ifipnext = ifi; /* prev points to this new one */ ifipnext = &ifi->ifi_next; /* pointer to next one goes here */ ifi->ifi_flags = flags; /* IFF_xxx values */ ifi->ifi_myflags = myflags; /* IFI_xxx values */ memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME); ifi->ifi_name[IFI_NAME-1] = '0'; switch (ifr->ifr_addr.sa_family) { case AF_INET: sinptr = (struct sockaddr_in *) &ifr->ifr_addr; if (ifi->ifi_addr == NULL) { ifi->ifi_addr = Calloc(1, sizeof(struct sockaddr_in)); memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in)); UNIX Network Programming 19
  • 20. lib/get_ifi_info.c (6/6) #ifdef SIOCGIFBRDADDR if (flags & IFF_BROADCAST) { Ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy); sinptr = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr; ifi->ifi_brdaddr = Calloc(1, sizeof(struct sockaddr_in)); memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in)); } #endif #ifdef SIOCGIFDSTADDR if (flags & IFF_POINTOPOINT) { Ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy); sinptr = (struct sockaddr_in *) &ifrcopy.ifr_dstaddr; ifi->ifi_dstaddr = Calloc(1, sizeof(struct sockaddr_in)); memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in)); } #endif } break; default: break; } } free(buf); return(ifihead); /* pointer to first structure in linked list */ } UNIX Network Programming 20
  • 21. Figure 16.10 free_ifi_info function: free dynamic memory allocated by get _ifi_info. void free_ifi_info(struct ifi_info *ifihead) { struct ifi_info *ifi, *ifinext; for (ifi = ifihead; ifi != NULL; ifi = ifinext) { if (ifi->ifi_addr != NULL) free(ifi->ifi_addr); if (ifi->ifi_brdaddr != NULL) free(ifi->ifi_brdaddr); if (ifi->ifi_dstaddr != NULL) free(ifi->ifi_dstaddr); ifinext = ifi->ifi_next; /* can't fetch ifi_next after free() */ free(ifi); /* the ifi_info{} itself */ } } UNIX Network Programming 21
  • 22. 16.7 Interface Operations • SIOCGIFADDR – return the unicast address in the ifr_addr member. • SIOCSIFADDR – sets the interface address from the ifr_addr member. • SIOCGIFFLAGS – return the interface flags in the ifr_flags member. • SIOCSIFFLAGS – sets the interface flags from the ifr_flags member. • SIOCGIFDSTADDR – return the point-to-point address in the ifr_dstaddr member. • SIOCSIFDSTADDR – set the point-to-point address from ifr_dstaddr member. UNIX Network Programming 22
  • 23. 16.7 Interface Operations • SIOCGIFBRDADDR – return the broadcast address in the ifr_broadaddr emember. • SIOCSIFBRDADDR – set the broadcast address from the ifr_boradaddr member. • SIOCGIFNETMASK – return the subnet mask in the ifr_addr member. • SIOCSIFNETMASK – set the subnet mask from the ifr_addr member. • SIOCGIFMETRIC – return the interface metric in the ifr_metric member. • SIOCSIFMETRIC – set the interface routing metric from the ifr_metric member. UNIX Network Programming 23
  • 24. 16.8 ARP Cache Operations struct arpreq { struct sockaddr arp_pa; /* protocol addesss */ struct sockaddr arp_ha; /* hardware address */ int arp_flags; /* flags */ }; #define ATF_INUSE 0x01 /* entry in use */ #define ATF_COM 0x02 /* completed entry (hardware addr valid) */ #define ATF_PERM 0x04 /* permanent entry */ #define ATF_PUBL 0x08 /* published entry (respond for other host) */ • SIOCSARP – add a new entry to the ARP cache or modify an existing entry. • SIOCDARP – delete an entry from the ARP cache. • SIOCGARP – get an entry from the ARP cache. UNIX Network Programming 24
  • 25. Figure 16.12 Print a host’s hardware addresses. int main(int argc, char **argv) { int family, sockfd; char str[INET6_ADDRSTRLEN]; char **pptr; unsigned char *ptr; struct arpreq arpreq; struct sockaddr_in *sin; pptr = my_addrs(&family); for ( ; *pptr != NULL; pptr++) { printf("%s: ", Inet_ntop(family, *pptr, str, sizeof(str))); switch (family) { case AF_INET: sockfd = Socket(AF_INET, SOCK_DGRAM, 0); sin = (struct sockaddr_in *) &arpreq.arp_pa; bzero(sin, sizeof(struct sockaddr_in)); sin->sin_family = AF_INET; memcpy(&sin->sin_addr, *pptr, sizeof(struct in_addr)); Ioctl(sockfd, SIOCGARP, &arpreq); ptr = &arpreq.arp_ha.sa_data[0]; printf("%x:%x:%x:%x:%x:%xn", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); break; default: err_quit("unsupported address family: %d", family); } } exit(0); } UNIX Network Programming 25
  • 26. 16.9 Routing Table Operations • Two ioctl requests are provided to operate on the routing table. • These two requests require that the third argument to ioctl be a pointer to an rtentry structure,which is defined by including the <net/route.h> header. • Only the superuser can issue these requests. • SIOCADDRT - add an entry to the routing table. • SIOCDELRT - delete an entry to the routing table. UNIX Network Programming 26