Improve protocol robustness and invoke user callbacks.
[libradsec.git] / lib / packet.c
1 /* See the file COPYING for licensing information.  */
2
3 #if defined HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <freeradius/libradius.h>
11 #include <event2/event.h>
12 #include <event2/bufferevent.h>
13 #include <radsec/radsec.h>
14 #include <radsec/radsec-impl.h>
15 #include "tls.h"
16 #include "debug.h"
17 #if defined (RS_ENABLE_TLS)
18 #include <event2/bufferevent_ssl.h>
19 #include <openssl/err.h>
20 #endif
21 #if defined (DEBUG)
22 #include <netdb.h>
23 #include <sys/socket.h>
24 #include <event2/buffer.h>
25 #endif
26
27 static int
28 _do_send (struct rs_packet *pkt)
29 {
30   int err;
31   VALUE_PAIR *vp;
32
33   assert (pkt->rpkt);
34   assert (!pkt->original);
35
36   vp = paircreate (PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
37   if (!vp)
38     return rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
39                                 "paircreate: %s", fr_strerror ());
40   pairadd (&pkt->rpkt->vps, vp);
41
42   if (rad_encode (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
43     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
44                                 "rad_encode: %s", fr_strerror ());
45   if (rad_sign (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
46     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
47                                 "rad_sign: %s", fr_strerror ());
48 #if defined (DEBUG)
49   {
50     char host[80], serv[80];
51
52     getnameinfo (pkt->conn->active_peer->addr->ai_addr,
53                  pkt->conn->active_peer->addr->ai_addrlen,
54                  host, sizeof(host), serv, sizeof(serv),
55                  0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
56     rs_debug (("%s: about to send this to %s:%s:\n", __func__, host, serv));
57     rs_dump_packet (pkt);
58   }
59 #endif
60
61   err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
62                            pkt->rpkt->data_len);
63   if (err < 0)
64     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
65                                 "bufferevent_write: %s",
66                                 evutil_gai_strerror(err));
67   return RSE_OK;
68 }
69
70 static void
71 _on_connect (struct rs_connection *conn)
72 {
73   conn->active_peer->is_connected = 1;
74   rs_debug (("%s: %p connected\n", __func__, conn->active_peer));
75   if (conn->callbacks.connected_cb)
76     conn->callbacks.connected_cb (conn->user_data);
77 }
78
79 static void
80 _on_disconnect (struct rs_connection *conn)
81 {
82   conn->active_peer->is_connected = 0;
83   rs_debug (("%s: %p disconnected\n", __func__, conn->active_peer));
84   if (conn->callbacks.disconnected_cb)
85     conn->callbacks.disconnected_cb (conn->user_data);
86 }
87
88 static void
89 _event_cb (struct bufferevent *bev, short events, void *ctx)
90 {
91   struct rs_packet *pkt = (struct rs_packet *)ctx;
92   struct rs_connection *conn;
93   struct rs_peer *p;
94   int sockerr;
95 #if defined (RS_ENABLE_TLS)
96   unsigned long tlserr;
97 #endif
98
99   assert (pkt);
100   assert (pkt->conn);
101   assert (pkt->conn->active_peer);
102   conn = pkt->conn;
103   p = conn->active_peer;
104
105   p->is_connecting = 0;
106   if (events & BEV_EVENT_CONNECTED)
107     {
108       _on_connect (conn);
109       if (_do_send (pkt))
110         rs_debug (("%s: error sending\n", __func__));
111     }
112   else if (events & BEV_EVENT_EOF)
113     {
114       _on_disconnect (conn);
115     }
116   else if (events & BEV_EVENT_ERROR)
117     {
118       sockerr = evutil_socket_geterror (conn->active_peer->fd);
119       if (sockerr == 0) /* FIXME: True that errno == 0 means closed? */
120         {
121           _on_disconnect (conn);
122         }
123       else
124         {
125           rs_err_conn_push_fl (pkt->conn, RSE_SOCKERR, __FILE__, __LINE__,
126                                "%d: socket error %d (%s)",
127                                conn->active_peer->fd,
128                                sockerr,
129                                evutil_socket_error_to_string (sockerr));
130           rs_debug (("%s: socket error on fd %d: %d\n", __func__,
131                      conn->active_peer->fd,
132                      sockerr));
133         }
134 #if defined (RS_ENABLE_TLS)
135       if (conn->tls_ssl)        /* FIXME: correct check?  */
136         {
137           for (tlserr = bufferevent_get_openssl_error (conn->bev);
138                tlserr;
139                tlserr = bufferevent_get_openssl_error (conn->bev))
140             {
141               rs_debug (("%s: openssl error: %s\n", __func__,
142                          ERR_error_string (tlserr, NULL)));
143               rs_err_conn_push_fl (pkt->conn, RSE_SSLERR, __FILE__, __LINE__,
144                                    "%d", tlserr);
145             }
146         }
147 #endif  /* RS_ENABLE_TLS */
148     }
149 }
150
151 static void
152 _write_cb (struct bufferevent *bev, void *ctx)
153 {
154   struct rs_packet *pkt = (struct rs_packet *) ctx;
155
156   assert (pkt);
157   assert (pkt->conn);
158
159   if (pkt->conn->callbacks.sent_cb)
160     pkt->conn->callbacks.sent_cb (pkt->conn->user_data);
161 }
162
163 /* Read one RADIUS packet header.  Return !0 on error.  A return value
164    of 0 means that we need more data.  */
165 static int
166 _read_header (struct rs_packet *pkt)
167 {
168   size_t n = 0;
169
170   n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
171   if (n == RS_HEADER_LEN)
172     {
173       pkt->hdr_read_flag = 1;
174       pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
175       if (pkt->rpkt->data_len < 20 || pkt->rpkt->data_len > 4096)
176         {
177           bufferevent_free (pkt->conn->bev); /* Close connection.  */
178           return rs_err_conn_push (pkt->conn, RSE_INVALID_PKT,
179                                    "invalid packet length: %d",
180                                    pkt->rpkt->data_len);
181         }
182       pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
183       if (!pkt->rpkt->data)
184         {
185           bufferevent_free (pkt->conn->bev); /* Close connection.  */
186           return rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
187                                       NULL);
188         }
189       memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
190       bufferevent_setwatermark (pkt->conn->bev, EV_READ,
191                                 pkt->rpkt->data_len - RS_HEADER_LEN, 0);
192       rs_debug (("%s: packet header read, total pkt len=%d\n",
193                  __func__, pkt->rpkt->data_len));
194     }
195   else if (n < 0)
196     {
197       rs_debug (("%s: buffer frozen while reading header\n", __func__));
198     }
199   else      /* Error: libevent gave us less than the low watermark. */
200     {
201       bufferevent_free (pkt->conn->bev); /* Close connection.  */
202       return rs_err_conn_push_fl (pkt->conn, RSE_INTERNAL, __FILE__, __LINE__,
203                                   "got %d octets reading header", n);
204     }
205
206   return 0;
207 }
208
209 static int
210 _read_packet (struct rs_packet *pkt)
211 {
212   size_t n = 0;
213
214   rs_debug (("%s: trying to read %d octets of packet data\n", __func__,
215              pkt->rpkt->data_len - RS_HEADER_LEN));
216
217   n = bufferevent_read (pkt->conn->bev,
218                         pkt->rpkt->data + RS_HEADER_LEN,
219                         pkt->rpkt->data_len - RS_HEADER_LEN);
220
221   rs_debug (("%s: read %ld octets of packet data\n", __func__, n));
222
223   if (n == pkt->rpkt->data_len - RS_HEADER_LEN)
224     {
225       bufferevent_disable (pkt->conn->bev, EV_READ);
226       rs_debug (("%s: complete packet read\n", __func__));
227       pkt->hdr_read_flag = 0;
228       memset (pkt->hdr, 0, sizeof(*pkt->hdr));
229
230       /* Checks done by rad_packet_ok:
231          - lenghts (FIXME: checks really ok for tcp?)
232          - invalid code field
233          - attribute lengths >= 2
234          - attribute sizes adding up correctly  */
235       if (!rad_packet_ok (pkt->rpkt, 0) != 0)
236         {
237           bufferevent_free (pkt->conn->bev); /* Close connection.  */
238           return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
239                                       "invalid packet: %s", fr_strerror ());
240         }
241
242       /* TODO: Verify that reception of an unsolicited response packet
243          results in connection being closed.  */
244
245       /* If we have a request to match this response against, verify
246          and decode the response.  */
247       if (pkt->original)
248         {
249           /* Verify header and message authenticator.  */
250           if (rad_verify (pkt->rpkt, pkt->original->rpkt,
251                           pkt->conn->active_peer->secret))
252             {
253               bufferevent_free (pkt->conn->bev); /* Close connection.  */
254               return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
255                                           "rad_verify: %s", fr_strerror ());
256             }
257
258           /* Decode and decrypt.  */
259           if (rad_decode (pkt->rpkt, pkt->original->rpkt,
260                           pkt->conn->active_peer->secret))
261             {
262               bufferevent_free (pkt->conn->bev); /* Close connection.  */
263               return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
264                                           "rad_decode: %s", fr_strerror ());
265             }
266         }
267
268 #if defined (DEBUG)
269       /* Find out what happens if there's data left in the buffer.  */
270       {
271         size_t rest = 0;
272         rest = evbuffer_get_length (bufferevent_get_input (pkt->conn->bev));
273         if (rest)
274           rs_debug (("%s: returning with %d octets left in buffer\n", __func__,
275                      rest));
276       }
277 #endif
278
279       /* Hand over message to user, changes ownership of pkt.  Don't
280          touch it afterwards -- it might have been freed.  */
281       if (pkt->conn->callbacks.received_cb)
282         pkt->conn->callbacks.received_cb (pkt, pkt->conn->user_data);
283     }
284   else if (n < 0)               /* Buffer frozen.  */
285     rs_debug (("%s: buffer frozen when reading packet\n", __func__));
286   else                          /* Short packet.  */
287     rs_debug (("%s: waiting for another %d octets\n", __func__,
288                pkt->rpkt->data_len - RS_HEADER_LEN - n));
289
290   return 0;
291 }
292
293 /* Read callback for TCP.
294
295    Read exactly one RADIUS message from BEV and store it in struct
296    rs_packet passed in CTX (hereby called 'pkt').
297
298    Verify the received packet against pkt->original, if !NULL.
299
300    Inform upper layer about successful reception of valid RADIUS
301    message by invoking conn->callbacks.recevied_cb(), if !NULL.  */
302 static void
303 _read_cb (struct bufferevent *bev, void *ctx)
304 {
305   struct rs_packet *pkt = (struct rs_packet *) ctx;
306
307   assert (pkt);
308   assert (pkt->conn);
309   assert (pkt->rpkt);
310
311   pkt->rpkt->sockfd = pkt->conn->active_peer->fd;
312   pkt->rpkt->vps = NULL;
313
314   if (!pkt->hdr_read_flag)
315     if (_read_header (pkt))
316       return;
317   _read_packet (pkt);
318 }
319
320 static void
321 _evlog_cb (int severity, const char *msg)
322 {
323   const char *sevstr;
324   switch (severity)
325     {
326     case _EVENT_LOG_DEBUG:
327 #if !defined (DEBUG_LEVENT)
328       return;
329 #endif
330       sevstr = "debug";
331       break;
332     case _EVENT_LOG_MSG:
333       sevstr = "msg";
334       break;
335     case _EVENT_LOG_WARN:
336       sevstr = "warn";
337       break;
338     case _EVENT_LOG_ERR:
339       sevstr = "err";
340       break;
341     default:
342       sevstr = "???";
343       break;
344     }
345   fprintf (stderr, "libevent: [%s] %s\n", sevstr, msg); /* FIXME: stderr?  */
346 }
347
348 static int
349 _init_evb (struct rs_connection *conn)
350 {
351   if (!conn->evb)
352     {
353 #if defined (DEBUG)
354       event_enable_debug_mode ();
355 #endif
356       event_set_log_callback (_evlog_cb);
357       conn->evb = event_base_new ();
358       if (!conn->evb)
359         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
360                                     "event_base_new");
361     }
362   return RSE_OK;
363 }
364
365 static int
366 _init_socket (struct rs_connection *conn, struct rs_peer *p)
367 {
368   if (p->fd != -1)
369     return RSE_OK;
370
371   assert (p->addr);
372   p->fd = socket (p->addr->ai_family, p->addr->ai_socktype,
373                   p->addr->ai_protocol);
374   if (p->fd < 0)
375     return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
376                                 strerror (errno));
377   if (evutil_make_socket_nonblocking (p->fd) < 0)
378     {
379       evutil_closesocket (p->fd);
380       return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
381                                   strerror (errno));
382     }
383   return RSE_OK;
384 }
385
386 static struct rs_peer *
387 _pick_peer (struct rs_connection *conn)
388 {
389   if (!conn->active_peer)
390     conn->active_peer = conn->peers;
391   return conn->active_peer;
392 }
393
394 static int
395 _init_bev (struct rs_connection *conn, struct rs_peer *peer)
396 {
397   if (conn->bev)
398     return RSE_OK;
399
400   switch (conn->type)
401     {
402     case RS_CONN_TYPE_UDP:
403     case RS_CONN_TYPE_TCP:
404       conn->bev = bufferevent_socket_new (conn->evb, peer->fd, 0);
405       if (!conn->bev)
406         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
407                                     "bufferevent_socket_new");
408       break;
409 #if defined (RS_ENABLE_TLS)
410     case RS_CONN_TYPE_TLS:
411       if (rs_tls_init (conn))
412         return -1;
413       /* Would be convenient to pass BEV_OPT_CLOSE_ON_FREE but things
414          seem to break when be_openssl_ctrl() (in libevent) calls
415          SSL_set_bio() after BIO_new_socket() with flag=1.  */
416       conn->bev =
417         bufferevent_openssl_socket_new (conn->evb, peer->fd, conn->tls_ssl,
418                                         BUFFEREVENT_SSL_CONNECTING, 0);
419       if (!conn->bev)
420         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
421                                     "bufferevent_openssl_socket_new");
422
423       break;
424     case RS_CONN_TYPE_DTLS:
425       return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
426                                   "%s: NYI", __func__);
427 #endif  /* RS_ENABLE_TLS */
428     default:
429       return rs_err_conn_push_fl (conn, RSE_INTERNAL, __FILE__, __LINE__,
430                                   "%s: unknown connection type: %d", __func__,
431                                   conn->type);
432     }
433
434   return RSE_OK;
435 }
436
437 static void
438 _do_connect (struct rs_peer *p)
439 {
440   int err;
441
442   err = bufferevent_socket_connect (p->conn->bev, p->addr->ai_addr,
443                                     p->addr->ai_addrlen);
444   if (err < 0)
445     rs_err_conn_push_fl (p->conn, RSE_EVENT, __FILE__, __LINE__,
446                          "bufferevent_socket_connect: %s",
447                          evutil_gai_strerror(err));
448   else
449     p->is_connecting = 1;
450 }
451
452 static int
453 _conn_open(struct rs_connection *conn, struct rs_packet *pkt)
454 {
455   struct rs_peer *p;
456
457   if (_init_evb (conn))
458     return -1;
459
460   p = _pick_peer (conn);
461   if (!p)
462     return rs_err_conn_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
463
464   if (_init_socket (conn, p))
465     return -1;
466
467   if (_init_bev (conn, p))
468     return -1;
469
470   if (!p->is_connected)
471     if (!p->is_connecting)
472       _do_connect (p);
473
474   return RSE_OK;
475 }
476
477 static int
478 _conn_is_open_p (struct rs_connection *conn)
479 {
480   return conn->active_peer && conn->active_peer->is_connected;
481 }
482
483 /* Public functions.  */
484 int
485 rs_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
486 {
487   struct rs_packet *p;
488   RADIUS_PACKET *rpkt;
489
490   *pkt_out = NULL;
491
492   rpkt = rad_alloc (1);
493   if (!rpkt)
494     return rs_err_conn_push (conn, RSE_NOMEM, __func__);
495   rpkt->id = conn->nextid++;
496
497   p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
498   if (!p)
499     {
500       rad_free (&rpkt);
501       return rs_err_conn_push (conn, RSE_NOMEM, __func__);
502     }
503   memset (p, 0, sizeof (struct rs_packet));
504   p->conn = conn;
505   p->rpkt = rpkt;
506
507   *pkt_out = p;
508   return RSE_OK;
509 }
510
511 int
512 rs_packet_create_auth_request (struct rs_connection *conn,
513                                struct rs_packet **pkt_out,
514                                const char *user_name, const char *user_pw)
515 {
516   struct rs_packet *pkt;
517   struct rs_attr *attr;
518
519   if (rs_packet_create (conn, pkt_out))
520     return -1;
521   pkt = *pkt_out;
522   pkt->rpkt->code = PW_AUTHENTICATION_REQUEST;
523
524   if (user_name)
525     {
526       if (rs_attr_create (conn, &attr, "User-Name", user_name))
527         return -1;
528       rs_packet_add_attr (pkt, attr);
529
530       if (user_pw)
531         {
532           if (rs_attr_create (conn, &attr, "User-Password", user_pw))
533             return -1;
534           rs_packet_add_attr (pkt, attr);
535         }
536     }
537
538   return RSE_OK;
539 }
540
541 /* User callback used when we're dispatching for user.  */
542 static void
543 _wcb (void *user_data)
544 {
545   struct rs_connection *conn = (struct rs_connection *) user_data;
546   int err;
547
548   assert (conn);
549
550   /* When we're running the event loop for the user, we must break
551      it in order to give the control back to the user.  */
552   err = event_base_loopbreak (conn->evb);
553   if (err < 0)
554     rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
555                          "event_base_loopbreak: %s",
556                          evutil_gai_strerror(err));
557 }
558
559 int
560 rs_packet_send (struct rs_packet *pkt, void *user_data)
561 {
562   struct rs_connection *conn = NULL;
563   int err = RSE_OK;
564
565   assert (pkt);
566   assert (pkt->conn);
567   conn = pkt->conn;
568
569   if (_conn_is_open_p (conn))
570     _do_send (pkt);
571   else
572     if (_conn_open (conn, pkt))
573       return -1;
574
575   assert (conn->evb);
576   assert (conn->bev);
577   assert (conn->active_peer);
578   assert (conn->active_peer->fd >= 0);
579
580   conn->user_data = user_data;
581   bufferevent_setcb (conn->bev, NULL, _write_cb, _event_cb, pkt);
582
583   /* Do dispatch, unless the user wants to do it herself.  */
584   if (!conn->user_dispatch_flag)
585     {
586       conn->callbacks.sent_cb = _wcb;
587       conn->user_data = conn;
588       rs_debug (("%s: entering event loop\n", __func__));
589       err = event_base_dispatch (conn->evb);
590       if (err < 0)
591         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
592                                     "event_base_dispatch: %s",
593                                     evutil_gai_strerror(err));
594       rs_debug (("%s: event loop done\n", __func__));
595       conn->callbacks.sent_cb = NULL;
596       conn->user_data = NULL;
597
598       if (!event_base_got_break (conn->evb))
599         return -1;
600     }
601
602   return RSE_OK;
603 }
604
605 static void
606 _rcb (struct rs_packet *packet, void *user_data)
607 {
608   int err = 0;
609
610   /* When we're running the event loop for the user, we must break it
611      in order to give the control back to the user.  */
612   err = event_base_loopbreak (packet->conn->evb);
613   if (err < 0)
614     rs_err_conn_push_fl (packet->conn, RSE_EVENT, __FILE__, __LINE__,
615                          "event_base_loopbreak: %s",
616                          evutil_gai_strerror(err));
617 }
618
619 /* Special function used in libradsec blocking dispatching mode,
620    i.e. with socket set to block on read/write and with no libradsec
621    callbacks registered.
622
623    For any other use of libradsec, a the received_cb callback should
624    be registered in the callbacks member of struct rs_connection.
625
626    On successful reception, verification and decoding of a RADIUS
627    message, PKT_OUT will upon return point at a pointer to a struct
628    rs_packet containing the message.
629
630    If anything goes wrong or if the read times out (TODO: explain),
631    PKT_OUT will point at the NULL pointer and one or more errors are
632    pushed on the connection (available through rs_err_conn_pop()).  */
633
634 int
635 rs_conn_receive_packet (struct rs_connection *conn,
636                         struct rs_packet *request,
637                         struct rs_packet **pkt_out)
638 {
639   int err = RSE_OK;
640   struct rs_packet *pkt = NULL;
641
642   assert (conn);
643   assert (!conn->user_dispatch_flag); /* Dispatching mode only.  */
644
645   if (rs_packet_create (conn, pkt_out))
646     return -1;
647   pkt = *pkt_out;
648   pkt->conn = conn;
649   pkt->original = request;
650
651   if (_conn_open (conn, pkt))
652     return -1;
653   assert (conn->evb);
654   assert (conn->bev);
655   assert (conn->active_peer);
656   assert (conn->active_peer->fd >= 0);
657
658   /* Install read and event callbacks with libevent.  */
659   bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
660   bufferevent_enable (conn->bev, EV_READ);
661   bufferevent_setcb (conn->bev, _read_cb, NULL, _event_cb, pkt);
662
663   /* Install read callback with ourselves, for signaling successful
664      reception of message.  */
665   conn->callbacks.received_cb = _rcb;
666
667   /* Dispatch.  */
668   rs_debug (("%s: entering event loop\n", __func__));
669   err = event_base_dispatch (conn->evb);
670   if (err < 0)
671     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
672                                 "event_base_dispatch: %s",
673                                 evutil_gai_strerror(err));
674   rs_debug (("%s: event loop done\n", __func__));
675   conn->callbacks.received_cb = NULL;
676   if (!event_base_got_break (conn->evb))
677     return -1;
678
679 #if defined (DEBUG)
680   rs_dump_packet (pkt);
681 #endif
682
683   pkt->original = NULL;         /* FIXME: Why?  */
684   return RSE_OK;
685 }
686
687 void
688 rs_packet_add_attr(struct rs_packet *pkt, struct rs_attr *attr)
689 {
690   pairadd (&pkt->rpkt->vps, attr->vp);
691   attr->pkt = pkt;
692 }
693
694 struct radius_packet *
695 rs_packet_frpkt(struct rs_packet *pkt)
696 {
697   assert (pkt);
698   return pkt->rpkt;
699 }
700
701 void
702 rs_packet_destroy(struct rs_packet *pkt)
703 {
704   if (pkt)
705     {
706       // FIXME: memory leak! TODO: free all attributes
707       rad_free (&pkt->rpkt);
708       rs_free (pkt->conn->ctx, pkt);
709     }
710 }