Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ldpd * Don't assume INET in connection path



details:   https://anonhg.NetBSD.org/src/rev/783e58aa370e
branches:  trunk
changeset: 784634:783e58aa370e
user:      kefren <kefren%NetBSD.org@localhost>
date:      Mon Feb 04 17:14:31 2013 +0000

description:
* Don't assume INET in connection path
* Lookup in hello list in order to get the correct LDP ID, instead of
  transport address
* Improve an error message

diffstat:

 usr.sbin/ldpd/ldp_peer.c  |   4 +-
 usr.sbin/ldpd/ldp_peer.h  |   3 +-
 usr.sbin/ldpd/pdu.c       |   7 +++--
 usr.sbin/ldpd/socketops.c |  61 ++++++++++++++++++++++++++++++++++------------
 4 files changed, 53 insertions(+), 22 deletions(-)

diffs (148 lines):

diff -r ea7d0ac49314 -r 783e58aa370e usr.sbin/ldpd/ldp_peer.c
--- a/usr.sbin/ldpd/ldp_peer.c  Mon Feb 04 15:44:45 2013 +0000
+++ b/usr.sbin/ldpd/ldp_peer.c  Mon Feb 04 17:14:31 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ldp_peer.c,v 1.9 2013/02/04 09:52:43 kefren Exp $ */
+/* $NetBSD: ldp_peer.c,v 1.10 2013/02/04 17:14:31 kefren Exp $ */
 
 /*
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
        myaddresses = NULL;
 }
 
-static int
+int
 sockaddr_cmp(const struct sockaddr *a, const struct sockaddr *b)
 {
        if (a->sa_len != b->sa_len || a->sa_family != b->sa_family)
diff -r ea7d0ac49314 -r 783e58aa370e usr.sbin/ldpd/ldp_peer.h
--- a/usr.sbin/ldpd/ldp_peer.h  Mon Feb 04 15:44:45 2013 +0000
+++ b/usr.sbin/ldpd/ldp_peer.h  Mon Feb 04 17:14:31 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ldp_peer.h,v 1.3 2013/01/26 17:29:55 kefren Exp $ */
+/* $NetBSD: ldp_peer.h,v 1.4 2013/02/04 17:14:31 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -86,6 +86,7 @@
 #define        LDP_PEER_ESTABLISHED    2
 #define        LDP_PEER_HOLDDOWN       3
 
+int    sockaddr_cmp(const struct sockaddr *, const struct sockaddr *);
 void            ldp_peer_init(void);
 struct ldp_peer *      ldp_peer_new(const struct in_addr *, struct sockaddr *,
                                struct sockaddr *, uint16_t, int);
diff -r ea7d0ac49314 -r 783e58aa370e usr.sbin/ldpd/pdu.c
--- a/usr.sbin/ldpd/pdu.c       Mon Feb 04 15:44:45 2013 +0000
+++ b/usr.sbin/ldpd/pdu.c       Mon Feb 04 17:14:31 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pdu.c,v 1.3 2013/01/28 21:35:35 kefren Exp $ */
+/* $NetBSD: pdu.c,v 1.4 2013/02/04 17:14:31 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -67,8 +67,9 @@
                return LDP_E_BAD_LENGTH;
 
        if (p->ldp_id.s_addr != rpdu->ldp_id.s_addr) {
-               fatalp("Invalid LDP ID received from %s\n",
-                   inet_ntoa(p->ldp_id));
+               fatalp("Invalid LDP ID %s received from ",
+                   inet_ntoa(rpdu->ldp_id));
+               fatalp("%s\n", inet_ntoa(p->ldp_id));
                notiftlv = build_notification(0,
                    NOTIF_FATAL | NOTIF_BAD_LDP_ID);
                send_tlv(p, (struct tlv *) notiftlv);
diff -r ea7d0ac49314 -r 783e58aa370e usr.sbin/ldpd/socketops.c
--- a/usr.sbin/ldpd/socketops.c Mon Feb 04 15:44:45 2013 +0000
+++ b/usr.sbin/ldpd/socketops.c Mon Feb 04 17:14:31 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: socketops.c,v 1.24 2013/02/03 19:41:59 kefren Exp $ */
+/* $NetBSD: socketops.c,v 1.25 2013/02/04 17:14:31 kefren Exp $ */
 
 /*
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -941,38 +941,67 @@
 void 
 new_peer_connection()
 {
-       struct sockaddr_in sa, sin_me;
+       union sockunion peer_address, my_address;
+       struct in_addr *peer_ldp_id = NULL;
+       struct hello_info *hi;
        int             s;
 
-       s = accept(ls, (struct sockaddr *) & sa,
-               & (socklen_t) { sizeof(struct sockaddr_in) } );
+       s = accept(ls, &peer_address.sa,
+               & (socklen_t) { sizeof(union sockunion) } );
        if (s < 0) {
                fatalp("accept: %s", strerror(errno));
                return;
        }
 
-       if (get_ldp_peer((const struct sockaddr *)&sa) != NULL) {
+       if (get_ldp_peer(&peer_address.sa) != NULL) {
+               close(s);
+               return;
+       }
+
+       warnp("Accepted a connection from %s\n", satos(&peer_address.sa));
+
+       if (getsockname(s, &my_address.sa,
+           & (socklen_t) { sizeof(union sockunion) } )) {
+               fatalp("new_peer_connection(): cannot getsockname\n");
+               close(s);
+               return;
+       }
+       if (peer_address.sa.sa_family == AF_INET)
+               peer_address.sin.sin_port = 0;
+       else if (peer_address.sa.sa_family == AF_INET6)
+               peer_address.sin6.sin6_port = 0;
+       else {
+               fatalp("Unknown peer address family\n");
                close(s);
                return;
        }
 
-       warnp("Accepted a connection from %s\n", inet_ntoa(sa.sin_addr));
-
-       if (getsockname(s, (struct sockaddr *)&sin_me,
-           & (socklen_t) { sizeof(struct sockaddr_in) } )) {
-               fatalp("new_peer_connection(): cannot getsockname\n");
+       /* Verify if it should connect - XXX: no check for INET6 */
+       if (peer_address.sa.sa_family == AF_INET &&
+           ntohl(peer_address.sin.sin_addr.s_addr) <
+           ntohl(my_address.sin.sin_addr.s_addr)) {
+               fatalp("Peer %s: connect from lower ID\n",
+                   satos(&peer_address.sa));
                close(s);
                return;
        }
 
-       if (ntohl(sa.sin_addr.s_addr) < ntohl(sin_me.sin_addr.s_addr)) {
-               fatalp("Peer %s: connect from lower ID\n",
-                   inet_ntoa(sa.sin_addr));
+       /* Match hello info in order to get ldp_id */
+       SLIST_FOREACH(hi, &hello_info_head, infos) {
+               if (sockaddr_cmp(&peer_address.sa,
+                   &hi->transport_address.sa) == 0) {
+                       peer_ldp_id = &hi->ldp_id;
+                       break;
+               }
+       }
+       if (peer_ldp_id == NULL) {
+               fatalp("Got connection from %s, but no hello info exists\n",
+                   satos(&peer_address.sa));
                close(s);
                return;
-       }
-       /* XXX: sa.sin_addr is not peer LDP ID ... */
-       ldp_peer_new(&sa.sin_addr, (struct sockaddr *)&sa, NULL, ldp_holddown_time, s);
+       } else
+               ldp_peer_new(peer_ldp_id, &peer_address.sa, NULL,
+                   ldp_holddown_time, s);
 
 }
 



Home | Main Index | Thread Index | Old Index