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

          Line data    Source code
       1             : /***
       2             :   This file is part of systemd.
       3             : 
       4             :   Copyright (C) 2014 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/types.h>
      22             : #include <sys/socket.h>
      23             : #include <string.h>
      24             : #include <linux/if_packet.h>
      25             : #include <stdio.h>
      26             : #include <unistd.h>
      27             : #include <netinet/ip6.h>
      28             : #include <netinet/icmp6.h>
      29             : #include <netinet/in.h>
      30             : 
      31             : #include "socket-util.h"
      32             : 
      33             : #include "dhcp6-internal.h"
      34             : #include "dhcp6-protocol.h"
      35             : 
      36             : #define IN6ADDR_ALL_ROUTERS_MULTICAST_INIT \
      37             :         { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
      38             :               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } } }
      39             : 
      40             : #define IN6ADDR_ALL_NODES_MULTICAST_INIT \
      41             :         { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
      42             :               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } }
      43             : 
      44           0 : int dhcp_network_icmp6_bind_router_solicitation(int index)
      45             : {
      46           0 :         struct icmp6_filter filter = { };
      47           0 :         struct ipv6_mreq mreq = {
      48             :                 .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
      49             :                 .ipv6mr_interface = index,
      50             :         };
      51           0 :         _cleanup_close_ int s = -1;
      52           0 :         int r, zero = 0, hops = 255;
      53             : 
      54           0 :         s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
      55             :                    IPPROTO_ICMPV6);
      56           0 :         if (s < 0)
      57           0 :                 return -errno;
      58             : 
      59           0 :         ICMP6_FILTER_SETBLOCKALL(&filter);
      60           0 :         ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
      61           0 :         r = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
      62             :                        sizeof(filter));
      63           0 :         if (r < 0)
      64           0 :                 return -errno;
      65             : 
      66             :         /* RFC 3315, section 6.7, bullet point 2 may indicate that an
      67             :            IPV6_PKTINFO socket option also applies for ICMPv6 multicast.
      68             :            Empirical experiments indicates otherwise and therefore an
      69             :            IPV6_MULTICAST_IF socket option is used here instead */
      70           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index,
      71             :                        sizeof(index));
      72           0 :         if (r < 0)
      73           0 :                 return -errno;
      74             : 
      75           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &zero,
      76             :                        sizeof(zero));
      77           0 :         if (r < 0)
      78           0 :                 return -errno;
      79             : 
      80           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,
      81             :                        sizeof(hops));
      82           0 :         if (r < 0)
      83           0 :                 return -errno;
      84             : 
      85           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq,
      86             :                        sizeof(mreq));
      87           0 :         if (r < 0)
      88           0 :                 return -errno;
      89             : 
      90           0 :         r = s;
      91           0 :         s = -1;
      92           0 :         return r;
      93             : }
      94             : 
      95           0 : int dhcp_network_icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr)
      96             : {
      97           0 :         struct sockaddr_in6 dst = {
      98             :                 .sin6_family = AF_INET6,
      99             :                 .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
     100             :         };
     101             :         struct {
     102             :                 struct nd_router_solicit rs;
     103             :                 struct nd_opt_hdr rs_opt;
     104             :                 struct ether_addr rs_opt_mac;
     105           0 :         } _packed_ rs = {
     106             :                 .rs.nd_rs_type = ND_ROUTER_SOLICIT,
     107             :         };
     108           0 :         struct iovec iov[1] = {
     109             :                 { &rs, },
     110             :         };
     111           0 :         struct msghdr msg = {
     112             :                 .msg_name = &dst,
     113             :                 .msg_namelen = sizeof(dst),
     114             :                 .msg_iov = iov,
     115             :                 .msg_iovlen = 1,
     116             :         };
     117             :         int r;
     118             : 
     119           0 :         if (ether_addr) {
     120           0 :                 memcpy(&rs.rs_opt_mac, ether_addr, ETH_ALEN);
     121           0 :                 rs.rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR;
     122           0 :                 rs.rs_opt.nd_opt_len = 1;
     123           0 :                 iov[0].iov_len = sizeof(rs);
     124             :         } else
     125           0 :                 iov[0].iov_len = sizeof(rs.rs);
     126             : 
     127           0 :         r = sendmsg(s, &msg, 0);
     128           0 :         if (r < 0)
     129           0 :                 return -errno;
     130             : 
     131           0 :         return 0;
     132             : }
     133             : 
     134           0 : int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
     135           0 :         struct in6_pktinfo pktinfo = {
     136             :                 .ipi6_ifindex = index,
     137             :         };
     138           0 :         union sockaddr_union src = {
     139             :                 .in6.sin6_family = AF_INET6,
     140           0 :                 .in6.sin6_port = htobe16(DHCP6_PORT_CLIENT),
     141             :                 .in6.sin6_addr = IN6ADDR_ANY_INIT,
     142             :         };
     143           0 :         _cleanup_close_ int s = -1;
     144           0 :         int r, off = 0, on = 1;
     145             : 
     146           0 :         if (local_address)
     147           0 :                 memcpy(&src.in6.sin6_addr, local_address,
     148             :                        sizeof(src.in6.sin6_addr));
     149             : 
     150           0 :         s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
     151             :                    IPPROTO_UDP);
     152           0 :         if (s < 0)
     153           0 :                 return -errno;
     154             : 
     155           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &pktinfo,
     156             :                        sizeof(pktinfo));
     157           0 :         if (r < 0)
     158           0 :                 return -errno;
     159             : 
     160           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
     161           0 :         if (r < 0)
     162           0 :                 return -errno;
     163             : 
     164           0 :         r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, sizeof(off));
     165           0 :         if (r < 0)
     166           0 :                 return -errno;
     167             : 
     168           0 :         r = bind(s, &src.sa, sizeof(src.in6));
     169           0 :         if (r < 0)
     170           0 :                 return -errno;
     171             : 
     172           0 :         r = s;
     173           0 :         s = -1;
     174           0 :         return r;
     175             : }
     176             : 
     177           0 : int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
     178             :                                   const void *packet, size_t len) {
     179           0 :         union sockaddr_union dest = {
     180             :                 .in6.sin6_family = AF_INET6,
     181           0 :                 .in6.sin6_port = htobe16(DHCP6_PORT_SERVER),
     182             :         };
     183             :         int r;
     184             : 
     185           0 :         assert(server_address);
     186             : 
     187           0 :         memcpy(&dest.in6.sin6_addr, server_address, sizeof(dest.in6.sin6_addr));
     188             : 
     189           0 :         r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in6));
     190           0 :         if (r < 0)
     191           0 :                 return -errno;
     192             : 
     193           0 :         return 0;
     194             : }

Generated by: LCOV version 1.11