LCOV - code coverage report
Current view: top level - libsystemd-network - dhcp-network.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 0 93 0.0 %
Date: 2015-07-29 18:47:03 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /***
       2             :   This file is part of systemd.
       3             : 
       4             :   Copyright (C) 2013 Intel Corporation. All rights reserved.
       5             : 
       6             :   systemd is free software; you can redistribute it and/or modify it
       7             :   under the terms of the GNU Lesser General Public License as published by
       8             :   the Free Software Foundation; either version 2.1 of the License, or
       9             :   (at your option) any later version.
      10             : 
      11             :   systemd is distributed in the hope that it will be useful, but
      12             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      14             :   Lesser General Public License for more details.
      15             : 
      16             :   You should have received a copy of the GNU Lesser General Public License
      17             :   along with systemd; If not, see <http://www.gnu.org/licenses/>.
      18             : ***/
      19             : 
      20             : #include <errno.h>
      21             : #include <sys/socket.h>
      22             : #include <string.h>
      23             : #include <linux/if_packet.h>
      24             : #include <linux/if_infiniband.h>
      25             : #include <net/ethernet.h>
      26             : #include <net/if_arp.h>
      27             : #include <stdio.h>
      28             : #include <linux/filter.h>
      29             : 
      30             : #include "socket-util.h"
      31             : 
      32             : #include "dhcp-internal.h"
      33             : 
      34           0 : static int _bind_raw_socket(int ifindex, union sockaddr_union *link,
      35             :                             uint32_t xid, const uint8_t *mac_addr,
      36             :                             size_t mac_addr_len,
      37             :                             const uint8_t *bcast_addr,
      38             :                             const struct ether_addr *eth_mac,
      39             :                             uint16_t arp_type, uint8_t dhcp_hlen) {
      40           0 :         struct sock_filter filter[] = {
      41             :                 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0),                                 /* A <- packet length */
      42             :                 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(DHCPPacket), 1, 0),         /* packet >= DHCPPacket ? */
      43             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      44             :                 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, ip.protocol)), /* A <- IP protocol */
      45             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 1, 0),                /* IP protocol == UDP ? */
      46             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      47             :                 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, ip.frag_off)), /* A <- Flags */
      48             :                 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x20),                             /* A <- A & 0x20 (More Fragments bit) */
      49             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0),                          /* A == 0 ? */
      50             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      51             :                 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, ip.frag_off)), /* A <- Flags + Fragment offset */
      52             :                 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x1fff),                           /* A <- A & 0x1fff (Fragment offset) */
      53             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0),                          /* A == 0 ? */
      54             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      55             :                 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, udp.dest)),    /* A <- UDP destination port */
      56             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_PORT_CLIENT, 1, 0),           /* UDP destination port == DHCP client port ? */
      57             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      58             :                 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.op)),     /* A <- DHCP op */
      59             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BOOTREPLY, 1, 0),                  /* op == BOOTREPLY ? */
      60             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      61             :                 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.htype)),  /* A <- DHCP header type */
      62             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arp_type, 1, 0),                   /* header type == arp_type ? */
      63             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      64             :                 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.hlen)),   /* A <- MAC address length */
      65             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, dhcp_hlen, 1, 0),                  /* address length == dhcp_hlen ? */
      66             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      67             :                 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.xid)),    /* A <- client identifier */
      68             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, xid, 1, 0),                        /* client identifier == xid ? */
      69             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      70           0 :                 BPF_STMT(BPF_LD + BPF_IMM, htobe32(*((unsigned int *) eth_mac))),                     /* A <- 4 bytes of client's MAC */
      71             :                 BPF_STMT(BPF_MISC + BPF_TAX, 0),                                                       /* X <- A */
      72             :                 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.chaddr)),                 /* A <- 4 bytes of MAC from dhcp.chaddr */
      73             :                 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0),                                                /* A xor X */
      74             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0),                                          /* A == 0 ? */
      75             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                                          /* ignore */
      76           0 :                 BPF_STMT(BPF_LD + BPF_IMM, htobe16(*((unsigned short *) (((char *) eth_mac) + 4)))),   /* A <- remainder of client's MAC */
      77             :                 BPF_STMT(BPF_MISC + BPF_TAX, 0),                                                       /* X <- A */
      78             :                 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, dhcp.chaddr) + 4),             /* A <- remainder of MAC from dhcp.chaddr */
      79             :                 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0),                                                /* A xor X */
      80             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0),                                          /* A == 0 ? */
      81             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                                          /* ignore */
      82             :                 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.magic)),  /* A <- DHCP magic cookie */
      83             :                 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_MAGIC_COOKIE, 1, 0),          /* cookie == DHCP magic cookie ? */
      84             :                 BPF_STMT(BPF_RET + BPF_K, 0),                                          /* ignore */
      85             :                 BPF_STMT(BPF_RET + BPF_K, 65535),                                      /* return all */
      86             :         };
      87           0 :         struct sock_fprog fprog = {
      88             :                 .len = ELEMENTSOF(filter),
      89             :                 .filter = filter
      90             :         };
      91           0 :         _cleanup_close_ int s = -1;
      92           0 :         int r, on = 1;
      93             : 
      94           0 :         assert(ifindex > 0);
      95           0 :         assert(link);
      96             : 
      97           0 :         s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
      98           0 :         if (s < 0)
      99           0 :                 return -errno;
     100             : 
     101           0 :         r = setsockopt(s, SOL_PACKET, PACKET_AUXDATA, &on, sizeof(on));
     102           0 :         if (r < 0)
     103           0 :                 return -errno;
     104             : 
     105           0 :         r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
     106           0 :         if (r < 0)
     107           0 :                 return -errno;
     108             : 
     109           0 :         link->ll.sll_family = AF_PACKET;
     110           0 :         link->ll.sll_protocol = htons(ETH_P_IP);
     111           0 :         link->ll.sll_ifindex = ifindex;
     112           0 :         link->ll.sll_hatype = htons(arp_type);
     113           0 :         link->ll.sll_halen = mac_addr_len;
     114           0 :         memcpy(link->ll.sll_addr, bcast_addr, mac_addr_len);
     115             : 
     116           0 :         r = bind(s, &link->sa, sizeof(link->ll));
     117           0 :         if (r < 0)
     118           0 :                 return -errno;
     119             : 
     120           0 :         r = s;
     121           0 :         s = -1;
     122             : 
     123           0 :         return r;
     124             : }
     125             : 
     126           0 : int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
     127             :                                  uint32_t xid, const uint8_t *mac_addr,
     128             :                                  size_t mac_addr_len, uint16_t arp_type) {
     129             :         static const uint8_t eth_bcast[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
     130             :         /* Default broadcast address for IPoIB */
     131             :         static const uint8_t ib_bcast[] = {
     132             :                 0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
     133             :                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     134             :                 0xff, 0xff, 0xff, 0xff
     135             :           };
     136           0 :         struct ether_addr eth_mac = { { 0, 0, 0, 0, 0, 0 } };
     137           0 :         const uint8_t *bcast_addr = NULL;
     138           0 :         uint8_t dhcp_hlen = 0;
     139             : 
     140           0 :         assert_return(mac_addr_len > 0, -EINVAL);
     141             : 
     142           0 :         if (arp_type == ARPHRD_ETHER) {
     143           0 :                 assert_return(mac_addr_len == ETH_ALEN, -EINVAL);
     144           0 :                 memcpy(&eth_mac, mac_addr, ETH_ALEN);
     145           0 :                 bcast_addr = eth_bcast;
     146           0 :                 dhcp_hlen = ETH_ALEN;
     147           0 :         } else if (arp_type == ARPHRD_INFINIBAND) {
     148           0 :                 assert_return(mac_addr_len == INFINIBAND_ALEN, -EINVAL);
     149           0 :                 bcast_addr = ib_bcast;
     150             :         } else
     151           0 :                 return -EINVAL;
     152             : 
     153           0 :         return _bind_raw_socket(ifindex, link, xid, mac_addr, mac_addr_len,
     154             :                                 bcast_addr, &eth_mac, arp_type, dhcp_hlen);
     155             : }
     156             : 
     157           0 : int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
     158           0 :         union sockaddr_union src = {
     159             :                 .in.sin_family = AF_INET,
     160           0 :                 .in.sin_port = htobe16(port),
     161             :                 .in.sin_addr.s_addr = address,
     162             :         };
     163           0 :         _cleanup_close_ int s = -1;
     164           0 :         int r, on = 1, tos = IPTOS_CLASS_CS6;
     165             : 
     166           0 :         s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
     167           0 :         if (s < 0)
     168           0 :                 return -errno;
     169             : 
     170           0 :         r = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
     171           0 :         if (r < 0)
     172           0 :                 return -errno;
     173             : 
     174           0 :         r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
     175           0 :         if (r < 0)
     176           0 :                 return -errno;
     177             : 
     178           0 :         if (address == INADDR_ANY) {
     179           0 :                 r = setsockopt(s, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
     180           0 :                 if (r < 0)
     181           0 :                         return -errno;
     182             : 
     183           0 :                 r = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
     184           0 :                 if (r < 0)
     185           0 :                         return -errno;
     186             :         } else {
     187           0 :                 r = setsockopt(s, IPPROTO_IP, IP_FREEBIND, &on, sizeof(on));
     188           0 :                 if (r < 0)
     189           0 :                         return -errno;
     190             :         }
     191             : 
     192           0 :         r = bind(s, &src.sa, sizeof(src.in));
     193           0 :         if (r < 0)
     194           0 :                 return -errno;
     195             : 
     196           0 :         r = s;
     197           0 :         s = -1;
     198             : 
     199           0 :         return r;
     200             : }
     201             : 
     202           0 : int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
     203             :                                  const void *packet, size_t len) {
     204             :         int r;
     205             : 
     206           0 :         assert(link);
     207           0 :         assert(packet);
     208           0 :         assert(len);
     209             : 
     210           0 :         r = sendto(s, packet, len, 0, &link->sa, sizeof(link->ll));
     211           0 :         if (r < 0)
     212           0 :                 return -errno;
     213             : 
     214           0 :         return 0;
     215             : }
     216             : 
     217           0 : int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
     218             :                                  const void *packet, size_t len) {
     219           0 :         union sockaddr_union dest = {
     220             :                 .in.sin_family = AF_INET,
     221           0 :                 .in.sin_port = htobe16(port),
     222             :                 .in.sin_addr.s_addr = address,
     223             :         };
     224             :         int r;
     225             : 
     226           0 :         assert(s >= 0);
     227           0 :         assert(packet);
     228           0 :         assert(len);
     229             : 
     230           0 :         r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in));
     231           0 :         if (r < 0)
     232           0 :                 return -errno;
     233             : 
     234           0 :         return 0;
     235             : }

Generated by: LCOV version 1.11