Import Upstream version 1.2.2
[quagga-debian.git] / bgpd / bgp_network.c
1 /* BGP network related fucntions
2    Copyright (C) 1999 Kunihiro Ishiguro
3
4 This file is part of GNU Zebra.
5
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 GNU Zebra 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING.  If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 #include <zebra.h>
22
23 #include "thread.h"
24 #include "sockunion.h"
25 #include "sockopt.h"
26 #include "memory.h"
27 #include "log.h"
28 #include "if.h"
29 #include "prefix.h"
30 #include "command.h"
31 #include "privs.h"
32 #include "linklist.h"
33 #include "network.h"
34 #include "filter.h"
35
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_fsm.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_debug.h"
40 #include "bgpd/bgp_network.h"
41
42 extern struct zebra_privs_t bgpd_privs;
43
44 /* BGP listening socket. */
45 struct bgp_listener
46 {
47   int fd;
48   union sockunion su;
49   struct thread *thread;
50 };
51
52 /*
53  * Set MD5 key for the socket, for the given IPv4 peer address.
54  * If the password is NULL or zero-length, the option will be disabled.
55  */
56 static int
57 bgp_md5_set_socket (int socket, union sockunion *su, const char *password)
58 {
59   int ret = -1;
60   int en = ENOSYS;
61   
62   assert (socket >= 0);
63   
64 #if HAVE_DECL_TCP_MD5SIG  
65   ret = sockopt_tcp_signature (socket, su, password);
66   en  = errno;
67 #endif /* HAVE_TCP_MD5SIG */
68   
69   if (ret < 0)
70     zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s",
71           socket, safe_strerror (en));
72
73   return ret;
74 }
75
76 /* Helper for bgp_connect */
77 static int
78 bgp_md5_set_connect (int socket, union sockunion *su, const char *password)
79 {
80   int ret = -1;
81
82 #if HAVE_DECL_TCP_MD5SIG  
83   if ( bgpd_privs.change (ZPRIVS_RAISE) )
84     {
85       zlog_err ("%s: could not raise privs", __func__);
86       return ret;
87     }
88   
89   ret = bgp_md5_set_socket (socket, su, password);
90
91   if (bgpd_privs.change (ZPRIVS_LOWER) )
92     zlog_err ("%s: could not lower privs", __func__);
93 #endif /* HAVE_TCP_MD5SIG */
94   
95   return ret;
96 }
97
98 int
99 bgp_md5_set (struct peer *peer)
100 {
101   struct listnode *node;
102   int ret = 0;
103   struct bgp_listener *listener;
104
105   if ( bgpd_privs.change (ZPRIVS_RAISE) )
106     {
107       zlog_err ("%s: could not raise privs", __func__);
108       return -1;
109     }
110   
111   /* Just set the password on the listen socket(s). Outbound connections
112    * are taken care of in bgp_connect() below.
113    */
114   for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
115     if (listener->su.sa.sa_family == peer->su.sa.sa_family)
116       {
117         ret = bgp_md5_set_socket (listener->fd, &peer->su, peer->password);
118         break;
119       }
120
121   if (bgpd_privs.change (ZPRIVS_LOWER) )
122     zlog_err ("%s: could not lower privs", __func__);
123   
124   return ret;
125 }
126
127 /* Update BGP socket send buffer size */
128 static void
129 bgp_update_sock_send_buffer_size (int fd)
130 {
131   int size = BGP_SOCKET_SNDBUF_SIZE;
132   int optval;
133   socklen_t optlen = sizeof(optval);
134
135   if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0)
136     {
137       zlog_err("getsockopt of SO_SNDBUF failed %s\n", safe_strerror(errno));
138       return;
139     }
140   if (optval < size)
141     {
142       if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
143         {
144           zlog_err("Couldn't increase send buffer: %s\n", safe_strerror(errno));
145         }
146     }
147 }
148
149 void
150 bgp_set_socket_ttl (struct peer *peer, int bgp_sock)
151 {
152   char buf[INET_ADDRSTRLEN];
153   int ret, ttl, minttl;
154
155   if (bgp_sock < 0)
156     return;
157
158   if (peer->gtsm_hops)
159     {
160       ttl = 255;
161       minttl = 256 - peer->gtsm_hops;
162     }
163   else
164     {
165       ttl = peer_ttl (peer);
166       minttl = 0;
167     }
168
169   ret = sockopt_ttl (peer->su.sa.sa_family, bgp_sock, ttl);
170   if (ret)
171     zlog_err ("%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
172               __func__,
173               inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
174               errno);
175
176   ret = sockopt_minttl (peer->su.sa.sa_family, bgp_sock, minttl);
177   if (ret && (errno != ENOTSUP || minttl))
178     zlog_err ("%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
179               __func__,
180               inet_ntop (AF_INET, &peer->remote_id, buf, sizeof(buf)),
181               errno);
182 }
183
184 /* Accept bgp connection. */
185 static int
186 bgp_accept (struct thread *thread)
187 {
188   int bgp_sock;
189   int accept_sock;
190   union sockunion su;
191   struct bgp_listener *listener = THREAD_ARG(thread);
192   struct peer *peer;
193   struct peer *peer1;
194   char buf[SU_ADDRSTRLEN];
195
196   /* Register accept thread. */
197   accept_sock = THREAD_FD (thread);
198   if (accept_sock < 0)
199     {
200       zlog_err ("accept_sock is nevative value %d", accept_sock);
201       return -1;
202     }
203   listener->thread = thread_add_read (bm->master, bgp_accept, listener, accept_sock);
204
205   /* Accept client connection. */
206   bgp_sock = sockunion_accept (accept_sock, &su);
207   if (bgp_sock < 0)
208     {
209       zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
210       return -1;
211     }
212   set_nonblocking (bgp_sock);
213
214   /* Set socket send buffer size */
215   bgp_update_sock_send_buffer_size(bgp_sock);
216
217   if (BGP_DEBUG (events, EVENTS))
218     zlog_debug ("[Event] BGP connection from host %s:%d", 
219                 inet_sutop (&su, buf), sockunion_get_port (&su));
220   
221   /* Check remote IP address */
222   peer1 = peer_lookup (NULL, &su);
223   /* We could perhaps just drop new connections from already Established
224    * peers here.
225    */
226   if (! peer1 || peer1->status == Idle || peer1->status > Established)
227     {
228       if (BGP_DEBUG (events, EVENTS))
229         {
230           if (! peer1)
231             zlog_debug ("[Event] BGP connection IP address %s is not configured",
232                        inet_sutop (&su, buf));
233           else
234             zlog_debug ("[Event] BGP connection IP address %s is %s state",
235                        inet_sutop (&su, buf),
236                        LOOKUP (bgp_status_msg, peer1->status));
237         }
238       close (bgp_sock);
239       return -1;
240     }
241
242   bgp_set_socket_ttl (peer1, bgp_sock);
243
244   /* Make dummy peer until read Open packet. */
245   if (BGP_DEBUG (events, EVENTS))
246     zlog_debug ("[Event] Make dummy peer structure until read Open packet");
247
248   {
249     char buf[SU_ADDRSTRLEN];
250
251     peer = peer_create_accept (peer1->bgp);
252     peer->su = su;
253     peer->fd = bgp_sock;
254     peer->status = Active;
255
256     /* Config state that should affect OPEN packet must be copied over */
257     peer->local_id = peer1->local_id;
258     peer->v_holdtime = peer1->v_holdtime;
259     peer->v_keepalive = peer1->v_keepalive;
260     peer->local_as = peer1->local_as;
261     peer->change_local_as = peer1->change_local_as;
262     peer->flags = peer1->flags;
263     peer->sflags = peer1->sflags;
264   #define PEER_ARRAY_COPY(D,S,A) \
265     memcpy ((D)->A, (S)->A, sizeof (((D)->A)[0][0])*AFI_MAX*SAFI_MAX);
266     PEER_ARRAY_COPY(peer, peer1, afc);
267     PEER_ARRAY_COPY(peer, peer1, af_flags);
268   #undef PEER_ARRAY_COPY
269   
270     /* Make peer's address string. */
271     sockunion2str (&su, buf, SU_ADDRSTRLEN);
272     peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
273     
274     SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
275   }
276
277   BGP_EVENT_ADD (peer, TCP_connection_open);
278
279   return 0;
280 }
281
282 /* BGP socket bind. */
283 static int
284 bgp_bind (struct peer *peer)
285 {
286 #ifdef SO_BINDTODEVICE
287   int ret;
288   struct ifreq ifreq;
289   int myerrno;
290
291   if (! peer->ifname)
292     return 0;
293
294   strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
295
296   if ( bgpd_privs.change (ZPRIVS_RAISE) )
297         zlog_err ("bgp_bind: could not raise privs");
298   
299   ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE, 
300                     &ifreq, sizeof (ifreq));
301   myerrno = errno;
302   
303   if (bgpd_privs.change (ZPRIVS_LOWER) )
304     zlog_err ("bgp_bind: could not lower privs");
305
306   if (ret < 0)
307     {
308       zlog (peer->log, LOG_INFO, "bind to interface %s failed, errno=%d",
309             peer->ifname, myerrno);
310       return ret;
311     }
312 #endif /* SO_BINDTODEVICE */
313   return 0;
314 }
315
316 static int
317 bgp_update_address (struct interface *ifp, const union sockunion *dst,
318                     union sockunion *addr)
319 {
320   struct prefix *p, *sel, d;
321   struct connected *connected;
322   struct listnode *node;
323   int common;
324
325   sockunion2hostprefix (dst, &d);
326   sel = NULL;
327   common = -1;
328
329   for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
330     {
331       p = connected->address;
332       if (p->family != d.family)
333         continue;
334       if (prefix_common_bits (p, &d) > common)
335         {
336           sel = p;
337           common = prefix_common_bits (sel, &d);
338         }
339     }
340
341   if (!sel)
342     return 1;
343
344   prefix2sockunion (sel, addr);
345   return 0;
346 }
347
348 /* Update source selection.  */
349 static void
350 bgp_update_source (struct peer *peer)
351 {
352   struct interface *ifp;
353   union sockunion addr;
354
355   /* Source is specified with interface name.  */
356   if (peer->update_if)
357     {
358       ifp = if_lookup_by_name (peer->update_if);
359       if (! ifp)
360         return;
361
362       if (bgp_update_address (ifp, &peer->su, &addr))
363         return;
364
365       sockunion_bind (peer->fd, &addr, 0, &addr);
366     }
367
368   /* Source is specified with IP address.  */
369   if (peer->update_source)
370     sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
371 }
372
373 /* BGP try to connect to the peer.  */
374 int
375 bgp_connect (struct peer *peer)
376 {
377   ifindex_t ifindex = 0;
378
379   /* Make socket for the peer. */
380   peer->fd = sockunion_socket (&peer->su);
381   if (peer->fd < 0)
382     return -1;
383
384   set_nonblocking (peer->fd);
385
386   /* Set socket send buffer size */
387   bgp_update_sock_send_buffer_size(peer->fd);
388
389   bgp_set_socket_ttl (peer, peer->fd);
390
391   sockopt_reuseaddr (peer->fd);
392   sockopt_reuseport (peer->fd);
393   
394 #ifdef IPTOS_PREC_INTERNETCONTROL
395   if (bgpd_privs.change (ZPRIVS_RAISE))
396     zlog_err ("%s: could not raise privs", __func__);
397   if (sockunion_family (&peer->su) == AF_INET)
398     setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
399   else if (sockunion_family (&peer->su) == AF_INET6)
400     setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
401   if (bgpd_privs.change (ZPRIVS_LOWER))
402     zlog_err ("%s: could not lower privs", __func__);
403 #endif
404
405   if (peer->password)
406     bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
407
408   /* Bind socket. */
409   bgp_bind (peer);
410
411   /* Update source bind. */
412   bgp_update_source (peer);
413
414   if (peer->ifname)
415     ifindex = ifname2ifindex (peer->ifname);
416
417   if (BGP_DEBUG (events, EVENTS))
418     plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
419                peer->host, peer->host, peer->fd);
420
421   /* Connect to the remote peer. */
422   return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
423 }
424
425 /* After TCP connection is established.  Get local address and port. */
426 void
427 bgp_getsockname (struct peer *peer)
428 {
429   if (peer->su_local)
430     {
431       sockunion_free (peer->su_local);
432       peer->su_local = NULL;
433     }
434
435   if (peer->su_remote)
436     {
437       sockunion_free (peer->su_remote);
438       peer->su_remote = NULL;
439     }
440
441   peer->su_local = sockunion_getsockname (peer->fd);
442   peer->su_remote = sockunion_getpeername (peer->fd);
443
444   bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
445 }
446
447
448 static int
449 bgp_listener (int sock, struct sockaddr *sa, socklen_t salen)
450 {
451   struct bgp_listener *listener;
452   int ret, en;
453
454   sockopt_reuseaddr (sock);
455   sockopt_reuseport (sock);
456
457   if (bgpd_privs.change (ZPRIVS_RAISE))
458     zlog_err ("%s: could not raise privs", __func__);
459
460 #ifdef IPTOS_PREC_INTERNETCONTROL
461   if (sa->sa_family == AF_INET)
462     setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
463   else if (sa->sa_family == AF_INET6)
464     setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
465 #endif
466
467   sockopt_v6only (sa->sa_family, sock);
468
469   ret = bind (sock, sa, salen);
470   en = errno;
471   if (bgpd_privs.change (ZPRIVS_LOWER))
472     zlog_err ("%s: could not lower privs", __func__);
473
474   if (ret < 0)
475     {
476       zlog_err ("bind: %s", safe_strerror (en));
477       return ret;
478     }
479
480   ret = listen (sock, 3);
481   if (ret < 0)
482     {
483       zlog_err ("listen: %s", safe_strerror (errno));
484       return ret;
485     }
486
487   listener = XMALLOC (MTYPE_BGP_LISTENER, sizeof(*listener));
488   listener->fd = sock;
489   memcpy(&listener->su, sa, salen);
490   listener->thread = thread_add_read (bm->master, bgp_accept, listener, sock);
491   listnode_add (bm->listen_sockets, listener);
492
493   return 0;
494 }
495
496 /* IPv6 supported version of BGP server socket setup.  */
497 int
498 bgp_socket (unsigned short port, const char *address)
499 {
500   struct addrinfo *ainfo;
501   struct addrinfo *ainfo_save;
502   static const struct addrinfo req = {
503     .ai_family = AF_UNSPEC,
504     .ai_flags = AI_PASSIVE,
505     .ai_socktype = SOCK_STREAM,
506   };
507   int ret, count;
508   char port_str[BUFSIZ];
509
510   snprintf (port_str, sizeof(port_str), "%d", port);
511   port_str[sizeof (port_str) - 1] = '\0';
512
513   ret = getaddrinfo (address, port_str, &req, &ainfo_save);
514   if (ret != 0)
515     {
516       zlog_err ("getaddrinfo: %s", gai_strerror (ret));
517       return -1;
518     }
519
520   count = 0;
521   for (ainfo = ainfo_save; ainfo; ainfo = ainfo->ai_next)
522     {
523       int sock;
524
525       if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
526         continue;
527      
528       sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
529       if (sock < 0)
530         {
531           zlog_err ("socket: %s", safe_strerror (errno));
532           continue;
533         }
534         
535       /* if we intend to implement ttl-security, this socket needs ttl=255 */
536       sockopt_ttl (ainfo->ai_family, sock, MAXTTL);
537       
538       ret = bgp_listener (sock, ainfo->ai_addr, ainfo->ai_addrlen);
539       if (ret == 0)
540         ++count;
541       else
542         close(sock);
543     }
544   freeaddrinfo (ainfo_save);
545   if (count == 0)
546     {
547       zlog_err ("%s: no usable addresses", __func__);
548       return -1;
549     }
550
551   return 0;
552 }
553
554 void
555 bgp_close (void)
556 {
557   struct listnode *node, *next;
558   struct bgp_listener *listener;
559
560   for (ALL_LIST_ELEMENTS (bm->listen_sockets, node, next, listener))
561     {
562       thread_cancel (listener->thread);
563       close (listener->fd);
564       listnode_delete (bm->listen_sockets, listener);
565       XFREE (MTYPE_BGP_LISTENER, listener);
566     }
567 }