[systemd-commits] 3 commits - src/libsystemd-network

Tom Gundersen tomegun at kemper.freedesktop.org
Tue May 6 13:40:09 PDT 2014


 src/libsystemd-network/dhcp-internal.h    |    2 
 src/libsystemd-network/dhcp-network.c     |   68 ++++++++++++++++++------------
 src/libsystemd-network/dhcp-packet.c      |    2 
 src/libsystemd-network/ipv4ll-network.c   |   22 ++++++---
 src/libsystemd-network/sd-dhcp-client.c   |    3 -
 src/libsystemd-network/test-dhcp-client.c |    2 
 6 files changed, 62 insertions(+), 37 deletions(-)

New commits:
commit c3d2994b595542caa971a06ea894019d16b95896
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue May 6 22:02:32 2014 +0200

    sd-dhcp: network - don't leak sockets on failure
    
    Also some general cleanups

diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c
index 2a63f18..dd26ef3 100644
--- a/src/libsystemd-network/dhcp-network.c
+++ b/src/libsystemd-network/dhcp-network.c
@@ -32,7 +32,8 @@
 
 #include "dhcp-internal.h"
 
-int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t xid) {
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link,
+                                 uint32_t xid) {
         struct sock_filter filter[] = {
             BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0),                                 /* A <- packet length */
             BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(DHCPPacket), 1, 0),         /* packet >= DHCPPacket ? */
@@ -66,7 +67,8 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t
             .len = ELEMENTSOF(filter),
             .filter = filter
         };
-        int s, one = 1;
+        _cleanup_close_ int s = -1;
+        int r, one = 1;
 
         assert(index > 0);
         assert(link);
@@ -75,74 +77,87 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t
         if (s < 0)
                 return -errno;
 
+        r = setsockopt (s, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one));
+        if (r < 0)
+                return -errno;
+
+        r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+        if (r < 0)
+                return -errno;
+
         link->ll.sll_family = AF_PACKET;
         link->ll.sll_protocol = htons(ETH_P_IP);
         link->ll.sll_ifindex =  index;
         link->ll.sll_halen = ETH_ALEN;
         memset(link->ll.sll_addr, 0xff, ETH_ALEN);
 
-        if (setsockopt (s, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one)) < 0)
-                return -errno;
-
-        if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
+        r = bind(s, &link->sa, sizeof(link->ll));
+        if (r < 0)
                 return -errno;
-        }
 
-        if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
-                safe_close(s);
-                return -errno;
-        }
+        r = s;
+        s = -1;
 
-        return s;
+        return r;
 }
 
-int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
-{
-        int s, tos = IPTOS_CLASS_CS6;
+int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
         union sockaddr_union src = {
                 .in.sin_family = AF_INET,
                 .in.sin_port = htobe16(port),
                 .in.sin_addr.s_addr = address,
         };
+        _cleanup_close_ int s = -1;
+        int r, tos = IPTOS_CLASS_CS6;
 
         s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
         if (s < 0)
                 return -errno;
 
-        if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
+        r = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
+        if (r < 0)
                 return -errno;
 
-        if (bind(s, &src.sa, sizeof(src.in)) < 0) {
-                safe_close(s);
+        r = bind(s, &src.sa, sizeof(src.in));
+        if (r < 0)
                 return -errno;
-        }
 
-        return s;
+        r = s;
+        s = -1;
+
+        return r;
 }
 
 int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
-                                 const void *packet, size_t len)
-{
+                                 const void *packet, size_t len) {
+        int r;
+
         assert(link);
         assert(packet);
         assert(len);
 
-        if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
+        r = sendto(s, packet, len, 0, &link->sa, sizeof(link->ll));
+        if (r < 0)
                 return -errno;
 
         return 0;
 }
 
 int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
-                                 const void *packet, size_t len)
-{
+                                 const void *packet, size_t len) {
         union sockaddr_union dest = {
                 .in.sin_family = AF_INET,
                 .in.sin_port = htobe16(port),
                 .in.sin_addr.s_addr = address,
         };
+        int r;
+
+        assert(s >= 0);
+        assert(packet);
+        assert(len);
 
-        if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
+        r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in));
+        if (r < 0)
                 return -errno;
 
         return 0;
diff --git a/src/libsystemd-network/ipv4ll-network.c b/src/libsystemd-network/ipv4ll-network.c
index b917fbf..0852f42 100644
--- a/src/libsystemd-network/ipv4ll-network.c
+++ b/src/libsystemd-network/ipv4ll-network.c
@@ -24,11 +24,14 @@
 
 int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
                                         const struct ether_arp *arp) {
+        int r;
+
         assert(arp);
         assert(link);
         assert(fd >= 0);
 
-        if (sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll)) < 0)
+        r = sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll));
+        if (r < 0)
                 return -errno;
 
         return 0;
@@ -56,7 +59,8 @@ int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
             .len = ELEMENTSOF(filter),
             .filter = filter
         };
-        int s;
+        _cleanup_close_ int s = -1;
+        int r;
 
         assert(index > 0);
         assert(link);
@@ -65,9 +69,9 @@ int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
         if (s < 0)
                 return -errno;
 
-        if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
+        r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+        if (r < 0)
                 return -errno;
-        }
 
         link->ll.sll_family = AF_PACKET;
         link->ll.sll_protocol = htons(ETH_P_ARP);
@@ -75,10 +79,12 @@ int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
         link->ll.sll_halen = ETH_ALEN;
         memset(link->ll.sll_addr, 0xff, ETH_ALEN);
 
-        if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
-                safe_close(s);
+        r = bind(s, &link->sa, sizeof(link->ll));
+        if (r < 0)
                 return -errno;
-        }
 
-        return s;
+        r = s;
+        s = -1;
+
+        return r;
 }

commit 85923f79e1d1191673c5dffa4bdf1f5aadaa415b
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue May 6 22:02:14 2014 +0200

    sd-dhcp: network - set TOS on outgoing packets
    
    This should improve performance on busy wireless networks and the
    like. Inspired by a similar change in dnsmasq.

diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c
index 722f1b1..2a63f18 100644
--- a/src/libsystemd-network/dhcp-network.c
+++ b/src/libsystemd-network/dhcp-network.c
@@ -98,7 +98,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t
 
 int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
 {
-        int s;
+        int s, tos = IPTOS_CLASS_CS6;
         union sockaddr_union src = {
                 .in.sin_family = AF_INET,
                 .in.sin_port = htobe16(port),
@@ -109,6 +109,9 @@ int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
         if (s < 0)
                 return -errno;
 
+        if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
+                return -errno;
+
         if (bind(s, &src.sa, sizeof(src.in)) < 0) {
                 safe_close(s);
                 return -errno;
diff --git a/src/libsystemd-network/dhcp-packet.c b/src/libsystemd-network/dhcp-packet.c
index b012e63..d72d7a6 100644
--- a/src/libsystemd-network/dhcp-packet.c
+++ b/src/libsystemd-network/dhcp-packet.c
@@ -116,6 +116,8 @@ void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
         packet->ip.ihl = DHCP_IP_SIZE / 4;
         packet->ip.tot_len = htobe16(len);
 
+        packet->ip.tos = IPTOS_CLASS_CS6;
+
         packet->ip.protocol = IPPROTO_UDP;
         packet->ip.saddr = source_addr;
         packet->ip.daddr = destination_addr;

commit 085cabf266bfbbc5ebdada9179a8ebe404e540a8
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue May 6 21:57:09 2014 +0200

    dhcp-network: don't pass ifindex to bind_udp_socket
    
    UDP sockets can anyway not be bound to specific netdev's. The packages would have to be filtered
    when received instead.

diff --git a/src/libsystemd-network/dhcp-internal.h b/src/libsystemd-network/dhcp-internal.h
index c0cfa83..eb4a6cd 100644
--- a/src/libsystemd-network/dhcp-internal.h
+++ b/src/libsystemd-network/dhcp-internal.h
@@ -30,7 +30,7 @@
 #include "dhcp-protocol.h"
 
 int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t xid);
-int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port);
+int dhcp_network_bind_udp_socket(be32_t address, uint16_t port);
 int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
                                  const void *packet, size_t len);
 int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c
index 902c9d9..722f1b1 100644
--- a/src/libsystemd-network/dhcp-network.c
+++ b/src/libsystemd-network/dhcp-network.c
@@ -96,7 +96,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t
         return s;
 }
 
-int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
+int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
 {
         int s;
         union sockaddr_union src = {
diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c
index 2ddb9ad..ab9f68d 100644
--- a/src/libsystemd-network/sd-dhcp-client.c
+++ b/src/libsystemd-network/sd-dhcp-client.c
@@ -770,8 +770,7 @@ static int client_timeout_t1(sd_event_source *s, uint64_t usec,
         client->state = DHCP_STATE_RENEWING;
         client->attempt = 1;
 
-        r = dhcp_network_bind_udp_socket(client->index,
-                                         client->lease->address,
+        r = dhcp_network_bind_udp_socket(client->lease->address,
                                          DHCP_PORT_CLIENT);
         if (r < 0) {
                 client_stop(client, r);
diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c
index 83dd619..599bda1 100644
--- a/src/libsystemd-network/test-dhcp-client.c
+++ b/src/libsystemd-network/test-dhcp-client.c
@@ -204,7 +204,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link, uint32_t
         return test_fd[0];
 }
 
-int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
+int dhcp_network_bind_udp_socket(be32_t address, uint16_t port)
 {
         return 0;
 }



More information about the systemd-commits mailing list