diff options
author | Ondrej Zajicek <santiago@crfreenet.org> | 2014-05-18 11:42:26 +0200 |
---|---|---|
committer | Ondrej Zajicek <santiago@crfreenet.org> | 2014-05-18 11:42:26 +0200 |
commit | 05476c4d04a24bdb26fa64e05ab31bc36118f34e (patch) | |
tree | e775f059cfb4bb027c444bb53eb9356e643082c8 /proto | |
parent | 1149aa977d906a6400f998d5f6600871584395d0 (diff) |
IPv4/IPv6 integrated socket code.
Diffstat (limited to 'proto')
-rw-r--r-- | proto/bfd/packets.c | 6 | ||||
-rw-r--r-- | proto/bgp/bgp.c | 81 | ||||
-rw-r--r-- | proto/ospf/iface.c | 20 | ||||
-rw-r--r-- | proto/ospf/packet.c | 4 | ||||
-rw-r--r-- | proto/radv/packets.c | 5 | ||||
-rw-r--r-- | proto/rip/rip.c | 11 |
6 files changed, 67 insertions, 60 deletions
diff --git a/proto/bfd/packets.c b/proto/bfd/packets.c index 964172d8..49b69bed 100644 --- a/proto/bfd/packets.c +++ b/proto/bfd/packets.c @@ -101,8 +101,8 @@ bfd_rx_hook(sock *sk, int len) uint err_val = 0; char fb[8]; - if ((sk->sport == BFD_CONTROL_PORT) && (sk->ttl < 255)) - DROP("wrong TTL", sk->ttl); + if ((sk->sport == BFD_CONTROL_PORT) && (sk->rcv_ttl < 255)) + DROP("wrong TTL", sk->rcv_ttl); if (len < BFD_BASE_LEN) DROP("too short", len); @@ -209,6 +209,7 @@ bfd_open_rx_sk(struct bfd_proto *p, int multihop) return sk; err: + sk_log_error(sk, p->p.name); rfree(sk); return NULL; } @@ -243,6 +244,7 @@ bfd_open_tx_sk(struct bfd_proto *p, ip_addr local, struct iface *ifa) return sk; err: + sk_log_error(sk, p->p.name); rfree(sk); return NULL; } diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index ca619f31..b6239971 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -106,14 +106,11 @@ bgp_open(struct bgp_proto *p) struct config *cfg = p->cf->c.global; int errcode; - bgp_counter++; - if (!bgp_listen_sk) bgp_listen_sk = bgp_setup_listen_sk(cfg->listen_bgp_addr, cfg->listen_bgp_port, cfg->listen_bgp_flags); if (!bgp_listen_sk) { - bgp_counter--; errcode = BEM_NO_SOCKET; goto err; } @@ -121,16 +118,16 @@ bgp_open(struct bgp_proto *p) if (!bgp_linpool) bgp_linpool = lp_new(&root_pool, 4080); + bgp_counter++; + if (p->cf->password) - { - int rv = sk_set_md5_auth(bgp_listen_sk, p->cf->remote_ip, p->cf->iface, p->cf->password); - if (rv < 0) - { - bgp_close(p, 0); - errcode = BEM_INVALID_MD5; - goto err; - } - } + if (sk_set_md5_auth(bgp_listen_sk, p->cf->remote_ip, p->cf->iface, p->cf->password) < 0) + { + sk_log_error(bgp_listen_sk, p->p.name); + bgp_close(p, 0); + errcode = BEM_INVALID_MD5; + goto err; + } return 0; @@ -194,7 +191,8 @@ bgp_close(struct bgp_proto *p, int apply_md5) bgp_counter--; if (p->cf->password && apply_md5) - sk_set_md5_auth(bgp_listen_sk, p->cf->remote_ip, p->cf->iface, NULL); + if (sk_set_md5_auth(bgp_listen_sk, p->cf->remote_ip, p->cf->iface, NULL) < 0) + sk_log_error(bgp_listen_sk, p->p.name); if (!bgp_counter) { @@ -697,25 +695,21 @@ bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing c bgp_conn_set_state(conn, BS_CONNECT); if (sk_open(s) < 0) - { - bgp_sock_err(s, 0); - return; - } + goto err; /* Set minimal receive TTL if needed */ if (p->cf->ttl_security) - { - DBG("Setting minimum received TTL to %d", 256 - hops); if (sk_set_min_ttl(s, 256 - hops) < 0) - { - log(L_ERR "TTL security configuration failed, closing session"); - bgp_sock_err(s, 0); - return; - } - } + goto err; DBG("BGP: Waiting for connect success\n"); bgp_start_timer(conn->connect_retry_timer, p->cf->connect_retry_time); + return; + + err: + sk_log_error(s, p->p.name); + bgp_sock_err(s, 0); + return; } /** @@ -760,32 +754,33 @@ bgp_incoming_connection(sock *sk, int dummy UNUSED) sk->dport, acc ? "accepted" : "rejected"); if (!acc) - goto err; + goto reject; int hops = p->cf->multihop ? : 1; + + if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0) + goto err; + if (p->cf->ttl_security) - { - /* TTL security support */ - if ((sk_set_ttl(sk, 255) < 0) || - (sk_set_min_ttl(sk, 256 - hops) < 0)) - { - log(L_ERR "TTL security configuration failed, closing session"); + if (sk_set_min_ttl(sk, 256 - hops) < 0) goto err; - } - } - else - sk_set_ttl(sk, hops); bgp_setup_conn(p, &p->incoming_conn); bgp_setup_sk(&p->incoming_conn, sk); bgp_send_open(&p->incoming_conn); return 0; + + err: + sk_log_error(sk, p->p.name); + log(L_ERR "%s: Incoming connection aborted", p->p.name); + rfree(sk); + return 0; } } log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)", sk->daddr, ipa_has_link_scope(sk->daddr) ? sk->iface : NULL, sk->dport); - err: + reject: rfree(sk); return 0; } @@ -816,13 +811,15 @@ bgp_setup_listen_sk(ip_addr addr, unsigned port, u32 flags) s->err_hook = bgp_listen_sock_err; if (sk_open(s) < 0) - { - log(L_ERR "BGP: Unable to open listening socket"); - rfree(s); - return NULL; - } + goto err; return s; + + err: + sk_log_error(s, "BGP"); + log(L_ERR "BGP: Cannot open listening socket"); + rfree(s); + return NULL; } static void diff --git a/proto/ospf/iface.c b/proto/ospf/iface.c index f4d9be55..50cf15e2 100644 --- a/proto/ospf/iface.c +++ b/proto/ospf/iface.c @@ -90,6 +90,8 @@ find_nbma_node_in(list *nnl, ip_addr ip) static int ospf_sk_open(struct ospf_iface *ifa) { + struct proto_ospf *po = ifa->oa->po; + sock *sk = sk_new(ifa->pool); sk->type = SK_IP; sk->dport = OSPF_PROTO; @@ -121,7 +123,7 @@ ospf_sk_open(struct ospf_iface *ifa) { ifa->all_routers = ifa->addr->brd; - if (sk_set_broadcast(sk, 1) < 0) + if (sk_setup_broadcast(sk) < 0) goto err; } else @@ -141,6 +143,7 @@ ospf_sk_open(struct ospf_iface *ifa) return 1; err: + sk_log_error(sk, po->proto.name); rfree(sk); return 0; } @@ -151,7 +154,9 @@ ospf_sk_join_dr(struct ospf_iface *ifa) if (ifa->sk_dr) return; - sk_join_group(ifa->sk, AllDRouters); + if (sk_join_group(ifa->sk, AllDRouters) < 0) + sk_log_error(ifa->sk, ifa->oa->po->proto.name); + ifa->sk_dr = 1; } @@ -161,15 +166,15 @@ ospf_sk_leave_dr(struct ospf_iface *ifa) if (!ifa->sk_dr) return; - sk_leave_group(ifa->sk, AllDRouters); + if (sk_leave_group(ifa->sk, AllDRouters) < 0) + sk_log_error(ifa->sk, ifa->oa->po->proto.name); + ifa->sk_dr = 0; } void ospf_open_vlink_sk(struct proto_ospf *po) { - struct proto *p = &po->proto; - sock *sk = sk_new(po->proto.pool); sk->type = SK_IP; sk->dport = OSPF_PROTO; @@ -197,8 +202,9 @@ ospf_open_vlink_sk(struct proto_ospf *po) return; err: + sk_log_error(sk, po->proto.name); + log(L_ERR "%s: Cannot open virtual link socket", po->proto.name); rfree(sk); - log(L_ERR "%s: Cannot open virtual link socket", p->name); } static void @@ -463,7 +469,7 @@ ospf_iface_add(struct object_lock *lock) /* Open socket if interface is not stub */ if (! ifa->stub && ! ospf_sk_open(ifa)) { - log(L_ERR "%s: Socket open failed on interface %s, declaring as stub", p->name, ifa->ifname); + log(L_ERR "%s: Cannot open socket for %s, declaring as stub", p->name, ifa->ifname); ifa->ioprob = OSPF_I_SK; ifa->stub = 1; } diff --git a/proto/ospf/packet.c b/proto/ospf/packet.c index cd4b8a97..1240b05c 100644 --- a/proto/ospf/packet.c +++ b/proto/ospf/packet.c @@ -308,9 +308,9 @@ ospf_rx_hook(sock *sk, int size) return 1; } - if (ifa->check_ttl && (sk->ttl < 255)) + if (ifa->check_ttl && (sk->rcv_ttl < 255)) { - log(L_ERR "%s%I - TTL %d (< 255)", mesg, sk->faddr, sk->ttl); + log(L_ERR "%s%I - TTL %d (< 255)", mesg, sk->faddr, sk->rcv_ttl); return 1; } diff --git a/proto/radv/packets.c b/proto/radv/packets.c index 997fda3d..1d7e04f4 100644 --- a/proto/radv/packets.c +++ b/proto/radv/packets.c @@ -416,11 +416,11 @@ radv_sk_open(struct radv_iface *ifa) sk->data = ifa; sk->flags = SKF_LADDR_RX; - if (sk_open(sk) != 0) + if (sk_open(sk) < 0) goto err; /* We want listen just to ICMPv6 messages of type RS and RA */ - if (sk_set_icmp_filter(sk, ICMPV6_RS, ICMPV6_RA) < 0) + if (sk_set_icmp6_filter(sk, ICMPV6_RS, ICMPV6_RA) < 0) goto err; if (sk_setup_multicast(sk) < 0) @@ -433,6 +433,7 @@ radv_sk_open(struct radv_iface *ifa) return 1; err: + sk_log_error(sk, ifa->ra->p.name); rfree(sk); return 0; } diff --git a/proto/rip/rip.c b/proto/rip/rip.c index 9730df77..bc9ffc5f 100644 --- a/proto/rip/rip.c +++ b/proto/rip/rip.c @@ -483,10 +483,10 @@ rip_rx(sock *s, int size) iface = i->iface; #endif - if (i->check_ttl && (s->ttl < 255)) + if (i->check_ttl && (s->rcv_ttl < 255)) { log( L_REMOTE "%s: Discarding packet with TTL %d (< 255) from %I on %s", - p->name, s->ttl, s->faddr, i->iface->name); + p->name, s->rcv_ttl, s->faddr, i->iface->name); return 1; } @@ -733,7 +733,7 @@ new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_ log( L_WARN "%s: interface %s is too strange for me", p->name, rif->iface->name ); } else { - if (sk_open(rif->sock)<0) + if (sk_open(rif->sock) < 0) goto err; if (rif->multicast) @@ -745,7 +745,7 @@ new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_ } else { - if (sk_set_broadcast(rif->sock, 1) < 0) + if (sk_setup_broadcast(rif->sock) < 0) goto err; } } @@ -755,7 +755,8 @@ new_iface(struct proto *p, struct iface *new, unsigned long flags, struct iface_ return rif; err: - log( L_ERR "%s: could not create socket for %s", p->name, rif->iface ? rif->iface->name : "(dummy)" ); + sk_log_error(rif->sock, p->name); + log(L_ERR "%s: Cannot open socket for %s", p->name, rif->iface ? rif->iface->name : "(dummy)" ); if (rif->iface) { rfree(rif->sock); mb_free(rif); |