Annotation of embedaddon/libnet/src/libnet_link_linux.c, revision 1.1.1.3

1.1       misho       1: /*
                      2:  *  $Id: libnet_link_linux.c,v 1.5 2004/01/03 20:31:02 mike Exp $
                      3:  *
                      4:  *  libnet 1.1
                      5:  *  libnet_link_linux.c - linux packet socket and pack socket routines
                      6:  *
                      7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                      8:  *  All rights reserved.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that: (1) source code distributions
                     12:  * retain the above copyright notice and this paragraph in its entirety, (2)
                     13:  * distributions including binary code include the above copyright notice and
                     14:  * this paragraph in its entirety in the documentation or other materials
                     15:  * provided with the distribution, and (3) all advertising materials mentioning
                     16:  * features or use of this software display the following acknowledgement:
                     17:  * ``This product includes software developed by the University of California,
                     18:  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
                     19:  * the University nor the names of its contributors may be used to endorse
                     20:  * or promote products derived from this software without specific prior
                     21:  * written permission.
                     22:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     23:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     24:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     25:  */
                     26: 
1.1.1.3 ! misho      27: #include "common.h"
        !            28: 
        !            29: 
1.1       misho      30: #include <sys/time.h>
                     31: 
                     32: #include <net/if.h>
                     33: #include <netinet/if_ether.h>
                     34: #include <net/if_arp.h>
                     35: 
                     36: #if (HAVE_PACKET_SOCKET)
                     37: #ifndef SOL_PACKET
                     38: #define SOL_PACKET 263
                     39: #endif  /* SOL_PACKET */
                     40: #include <netpacket/packet.h>
                     41: #include <net/ethernet.h>     /* the L2 protocols */
                     42: #endif  /* HAVE_PACKET_SOCKET */
                     43: 
                     44: #include "../include/libnet.h"
                     45: 
1.1.1.2   misho      46: /* These should not vary across linux systems, and are only defined in
                     47:  * <pcap-bpf.h>, included from <pcap.h>, but since we have no other dependency
                     48:  * on libpcap right now, define locally. I'm not sure if this is a good idea,
                     49:  * but we'll try.
                     50:  */
                     51: #define DLT_PRONET     4       /* Proteon ProNET Token Ring */
                     52: #define DLT_FDDI       10      /* FDDI */
                     53: #define DLT_RAW                12      /* raw IP */
                     54: 
1.1       misho      55: #include "../include/gnuc.h"
                     56: #ifdef HAVE_OS_PROTO_H
                     57: #include "../include/os-proto.h"
                     58: #endif
                     59: 
                     60: 
                     61: int
                     62: libnet_open_link(libnet_t *l)
                     63: {
                     64:     struct ifreq ifr;
                     65:     int n = 1;
                     66: 
                     67:     if (l == NULL)
                     68:     { 
                     69:         return (-1);
                     70:     } 
                     71: 
                     72: #if (HAVE_PACKET_SOCKET)
                     73:     l->fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
                     74: #else
                     75:     l->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
                     76: #endif
                     77:     if (l->fd == -1)
                     78:     {
1.1.1.2   misho      79:         if (errno == EPERM) {
                     80:             snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     81:                      "%s(): UID/EUID 0 or capability CAP_NET_RAW required",
                     82:                      __func__);
                     83: 
                     84:         } else {
                     85:             snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     86:                      "socket: %s", strerror(errno));
                     87:         }
1.1       misho      88:         goto bad;
                     89:     }
                     90: 
                     91:     memset(&ifr, 0, sizeof (ifr));
                     92:     strncpy(ifr.ifr_name, l->device, sizeof (ifr.ifr_name) -1);
                     93:     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
                     94: 
                     95:     if (ioctl(l->fd, SIOCGIFHWADDR, &ifr) < 0 )
                     96:     {
                     97:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                     98:                 "SIOCGIFHWADDR: %s", strerror(errno));
                     99:         goto bad;
                    100:     }
                    101: 
                    102:     switch (ifr.ifr_hwaddr.sa_family)
                    103:     {
                    104:         case ARPHRD_ETHER:
                    105:         case ARPHRD_METRICOM:
                    106: #ifdef ARPHRD_LOOPBACK
                    107:         case ARPHRD_LOOPBACK:   
                    108: #endif
                    109:             l->link_type = DLT_EN10MB;
                    110:             l->link_offset = 0xe;
                    111:             break;
                    112:         case ARPHRD_SLIP:
                    113:         case ARPHRD_CSLIP:
                    114:         case ARPHRD_SLIP6:
                    115:         case ARPHRD_CSLIP6:
                    116:         case ARPHRD_PPP:
1.1.1.3 ! misho     117:         case ARPHRD_NONE:
1.1       misho     118:             l->link_type = DLT_RAW;
                    119:             break;
                    120:         case ARPHRD_FDDI:
                    121:             l->link_type   = DLT_FDDI;
                    122:             l->link_offset = 0x15;
                    123:             break;
                    124:         /* Token Ring */
                    125:         case ARPHRD_IEEE802:
                    126:         case ARPHRD_IEEE802_TR:
                    127:         case ARPHRD_PRONET:
                    128:             l->link_type   = DLT_PRONET;
                    129:             l->link_offset = 0x16;
                    130:             break;
                    131: 
                    132:         default:
                    133:             snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho     134:                 "unknown physical layer type 0x%x",
1.1       misho     135:                 ifr.ifr_hwaddr.sa_family);
                    136:         goto bad;
                    137:     }
                    138: #ifdef SO_BROADCAST
                    139: /*
                    140:  * man 7 socket
                    141:  *
                    142:  * Set or get the broadcast flag. When  enabled,  datagram  sockets
                    143:  * receive packets sent to a broadcast address and they are allowed
                    144:  * to send packets to a broadcast  address.   This  option  has  no
                    145:  * effect on stream-oriented sockets.
                    146:  */
                    147:     if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1)
                    148:     {
                    149:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho     150:                 "%s: set SO_BROADCAST failed: %s",
1.1       misho     151:                 __func__, strerror(errno));
                    152:         goto bad;
                    153:     }
                    154: #endif  /*  SO_BROADCAST  */
                    155: 
                    156:     return (1);
                    157: 
                    158: bad:
                    159:     if (l->fd >= 0)
                    160:     {
                    161:         close(l->fd);
                    162:     }
                    163:     return (-1);
                    164: }
                    165: 
                    166: 
                    167: int
                    168: libnet_close_link(libnet_t *l)
                    169: {
                    170:     if (close(l->fd) == 0)
                    171:     {
                    172:         return (1);
                    173:     }
                    174:     else
                    175:     {
                    176:         return (-1);
                    177:     }
                    178: }
                    179: 
                    180: 
                    181: #if (HAVE_PACKET_SOCKET)
                    182: static int
1.1.1.2   misho     183: get_iface_index(int fd, const char *device)
1.1       misho     184: {
                    185:     struct ifreq ifr;
                    186:  
                    187:     /* memset(&ifr, 0, sizeof(ifr)); */
                    188:     strncpy (ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
                    189:     ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
                    190:  
                    191:     if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1)
                    192:     {
                    193:         return (-1);
                    194:     }
                    195:  
                    196:     return ifr.ifr_ifindex;
                    197: }
                    198: #endif
                    199: 
                    200: 
                    201: int
1.1.1.2   misho     202: libnet_write_link(libnet_t *l, const uint8_t *packet, uint32_t size)
1.1       misho     203: {
                    204:     int c;
                    205: #if (HAVE_PACKET_SOCKET)
                    206:     struct sockaddr_ll sa;
                    207: #else
                    208:     struct sockaddr sa;
                    209: #endif
                    210: 
                    211:     if (l == NULL)
                    212:     { 
                    213:         return (-1);
                    214:     }
                    215: 
                    216:     memset(&sa, 0, sizeof (sa));
                    217: #if (HAVE_PACKET_SOCKET)
                    218:     sa.sll_family    = AF_PACKET;
                    219:     sa.sll_ifindex   = get_iface_index(l->fd, l->device);
                    220:     if (sa.sll_ifindex == -1)
                    221:     {
                    222:         return (-1);
                    223:     }
                    224:     sa.sll_protocol  = htons(ETH_P_ALL);
                    225: #else
                    226:        strncpy(sa.sa_data, l->device, sizeof (sa.sa_data) - 1);
                    227:     sa.sa_data[sizeof (sa.sa_data) - 1] = 0;
                    228: #endif
                    229: 
                    230:     c = sendto(l->fd, packet, size, 0,
                    231:             (struct sockaddr *)&sa, sizeof (sa));
                    232:     if (c != size)
                    233:     {
                    234:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho     235:                 "libnet_write_link(): only %d bytes written (%s)", c,
1.1       misho     236:                 strerror(errno));
                    237:     }
                    238:     return (c);
                    239: }
                    240: 
                    241: 
                    242: struct libnet_ether_addr *
                    243: libnet_get_hwaddr(libnet_t *l)
                    244: {
                    245:     int fd;
                    246:     struct ifreq ifr;
                    247: 
                    248:     if (l == NULL)
                    249:     { 
                    250:         return (NULL);
                    251:     } 
                    252: 
                    253:     if (l->device == NULL)
                    254:     {           
                    255:         if (libnet_select_device(l) == -1)
                    256:         {   
                    257:             snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho     258:                     "libnet_get_hwaddr: can't figure out a device to use");
1.1       misho     259:             return (NULL);
                    260:         }
                    261:     }
                    262: 
                    263:     /*
                    264:      *  Create dummy socket to perform an ioctl upon.
                    265:      */
                    266:     fd = socket(AF_INET, SOCK_DGRAM, 0);
                    267:     if (fd < 0)
                    268:     {
                    269:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    270:                 "socket: %s", strerror(errno));
                    271:         goto bad;
                    272:     }
                    273: 
                    274:     memset(&ifr, 0, sizeof(ifr));
1.1.1.3 ! misho     275:     strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) - 1);
1.1       misho     276:     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
                    277: 
1.1.1.3 ! misho     278:     if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
1.1       misho     279:     {
                    280:         close(fd);
                    281:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    282:                 "ioctl: %s", strerror(errno));
                    283:         goto bad;
                    284:     }
                    285:     close(fd);
1.1.1.3 ! misho     286: 
        !           287:     return memcpy(l->link_addr.ether_addr_octet, &ifr.ifr_hwaddr.sa_data,
        !           288:                   ETHER_ADDR_LEN);
1.1       misho     289: 
                    290: bad:
                    291:     return (NULL);
                    292: }
                    293: 
1.1.1.3 ! misho     294: /**
1.1.1.2   misho     295:  * Local Variables:
1.1.1.3 ! misho     296:  *  indent-tabs-mode: nil
        !           297:  *  c-file-style: "stroustrup"
1.1.1.2   misho     298:  * End:
                    299:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>