]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppol2tp/openl2tp.c
Use autoconf/automake to configure and make ppp
[ppp.git] / pppd / plugins / pppol2tp / openl2tp.c
1 /*****************************************************************************
2  * Copyright (C) 2006,2007,2008 Katalix Systems Ltd
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *****************************************************************************/
19
20 /* pppd plugin for interfacing to openl2tpd */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include "pppd.h"
31 #include "pathnames.h"
32 #include "fsm.h"
33 #include "lcp.h"
34 #include "ccp.h"
35 #include "ipcp.h"
36 #include <sys/stat.h>
37 #include <net/if.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/un.h>
41 #include <netinet/in.h>
42 #include <signal.h>
43 #include <linux/version.h>
44 #include <linux/sockios.h>
45
46 #ifndef aligned_u64
47 /* should be defined in sys/types.h */
48 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
49 #endif
50 #include <linux/types.h>
51 #include <linux/if_ether.h>
52 #include <linux/ppp_defs.h>
53 #include <linux/if_ppp.h>
54 #include <linux/if_pppox.h>
55 #include <linux/if_pppol2tp.h>
56
57 #include "l2tp_event.h"
58
59 extern int pppol2tp_tunnel_id;
60 extern int pppol2tp_session_id;
61
62 extern void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
63                                        uint32_t send_accm, uint32_t recv_accm);
64 extern void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up);
65
66 const char pppd_version[] = VERSION;
67
68 static int openl2tp_fd = -1;
69
70 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
71                                            uint32_t send_accm,
72                                            uint32_t recv_accm) = NULL;
73 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
74                                            int up) = NULL;
75 static void (*old_multilink_join_hook)(void) = NULL;
76
77 /*****************************************************************************
78  * OpenL2TP interface.
79  * We send a PPP_ACCM_IND to openl2tpd to report ACCM values and
80  * SESSION_PPP_UPDOWN_IND to indicate when the PPP link comes up or
81  * goes down.
82  *****************************************************************************/
83
84 static int openl2tp_client_create(void)
85 {
86         struct sockaddr_un addr;
87         int result;
88
89         if (openl2tp_fd < 0) {
90                 openl2tp_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
91                 if (openl2tp_fd < 0) {
92                         error("openl2tp connection create: %m");
93                         return -ENOTCONN;
94                 }
95
96                 addr.sun_family = AF_UNIX;
97                 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
98
99                 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
100                                  sizeof(addr));
101                 if (result < 0) {
102                         error("openl2tp connection connect: %m");
103                         return -ENOTCONN;
104                 }
105         }
106
107         return 0;
108 }
109
110 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
111                                    uint32_t send_accm, uint32_t recv_accm)
112 {
113         int result;
114         uint8_t buf[OPENL2TP_MSG_MAX_LEN];
115         struct openl2tp_event_msg *msg = (void *) &buf[0];
116         struct openl2tp_event_tlv *tlv;
117         uint16_t tid = tunnel_id;
118         uint16_t sid = session_id;
119         struct openl2tp_tlv_ppp_accm accm;
120
121         if (openl2tp_fd < 0) {
122                 result = openl2tp_client_create();
123                 if (result < 0) {
124                         goto out;
125                 }
126         }
127
128         accm.send_accm = send_accm;
129         accm.recv_accm = recv_accm;
130
131         msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
132         msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
133         msg->msg_len = 0;
134
135         tlv = (void *) &msg->msg_data[msg->msg_len];
136         tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
137         tlv->tlv_len = sizeof(tid);
138         memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
139         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
140
141         tlv = (void *) &msg->msg_data[msg->msg_len];
142         tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
143         tlv->tlv_len = sizeof(sid);
144         memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
145         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
146
147         tlv = (void *) &msg->msg_data[msg->msg_len];
148         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_ACCM;
149         tlv->tlv_len = sizeof(accm);
150         memcpy(&tlv->tlv_value[0], &accm, tlv->tlv_len);
151         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
152
153         result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
154                       MSG_NOSIGNAL);
155         if (result < 0) {
156                 error("openl2tp send: %m");
157         }
158         if (result != (sizeof(*msg) + msg->msg_len)) {
159                 warn("openl2tp send: unexpected byte count %d, expected %d",
160                      result, sizeof(msg) + msg->msg_len);
161         }
162         dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
163
164 out:
165         if (old_pppol2tp_send_accm_hook != NULL) {
166                 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
167                                                send_accm, recv_accm);
168         }
169         return;
170 }
171
172 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
173 {
174         int result;
175         uint8_t buf[OPENL2TP_MSG_MAX_LEN];
176         struct openl2tp_event_msg *msg = (void *) &buf[0];
177         struct openl2tp_event_tlv *tlv;
178         uint16_t tid = tunnel_id;
179         uint16_t sid = session_id;
180         uint8_t state = up;
181         int unit = ifunit;
182         char *user_name = NULL;
183
184         if (openl2tp_fd < 0) {
185                 result = openl2tp_client_create();
186                 if (result < 0) {
187                         goto out;
188                 }
189         }
190
191         if (peer_authname[0] != '\0') {
192                 user_name = strdup(peer_authname);
193         }
194
195         msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
196         msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
197         msg->msg_len = 0;
198
199         tlv = (void *) &msg->msg_data[msg->msg_len];
200         tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
201         tlv->tlv_len = sizeof(tid);
202         memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
203         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
204
205         tlv = (void *) &msg->msg_data[msg->msg_len];
206         tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
207         tlv->tlv_len = sizeof(sid);
208         memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
209         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
210
211         tlv = (void *) &msg->msg_data[msg->msg_len];
212         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_STATE;
213         tlv->tlv_len = sizeof(state);
214         memcpy(&tlv->tlv_value[0], &state, tlv->tlv_len);
215         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
216
217         tlv = (void *) &msg->msg_data[msg->msg_len];
218         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_UNIT;
219         tlv->tlv_len = sizeof(unit);
220         memcpy(&tlv->tlv_value[0], &unit, tlv->tlv_len);
221         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
222
223         tlv = (void *) &msg->msg_data[msg->msg_len];
224         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_IFNAME;
225         tlv->tlv_len = strlen(ifname) + 1;
226         memcpy(&tlv->tlv_value[0], ifname, tlv->tlv_len);
227         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
228
229         if (user_name != NULL) {
230                 tlv = (void *) &msg->msg_data[msg->msg_len];
231                 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_USER_NAME;
232                 tlv->tlv_len = strlen(user_name) + 1;
233                 memcpy(&tlv->tlv_value[0], user_name, tlv->tlv_len);
234                 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
235         }
236
237         result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
238                       MSG_NOSIGNAL);
239         if (result < 0) {
240                 error("openl2tp send: %m");
241         }
242         if (result != (sizeof(*msg) + msg->msg_len)) {
243                 warn("openl2tp send: unexpected byte count %d, expected %d",
244                      result, sizeof(msg) + msg->msg_len);
245         }
246         dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
247
248 out:
249         if (old_pppol2tp_ip_updown_hook != NULL) {
250                 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
251         }
252
253         if (user_name != NULL)
254                 free(user_name);
255
256         return;
257 }
258
259 /*****************************************************************************
260  * When a multilink interface is created, there are 2 cases to consider.
261  *
262  * 1. The new interface is the first of a multilink bundle (master).
263  * 2. The new interface is being attached to an existing bundle.
264  *
265  * The first case is handled by existing code because the interface
266  * generates ip-up events just like standard interfaces. But in the
267  * second case, where the interface is added to an existing ppp
268  * bundle, pppd does not do IP negotiation and so as a result, no
269  * ip-up event is generated when the interface is created. Since
270  * openl2tpd needs the SESSION_PPP_UPDOWN_IND for all interfaces of a
271  * PPP bundle, we must fake the event.
272  *
273  * We use the ip_multilink_join_hook to hear when an interface joins a
274  * multilink bundle.
275  *****************************************************************************/
276
277 static void openl2tp_multilink_join_ind(void)
278 {
279         if (doing_multilink && !multilink_master) {
280                 /* send event only if not master */
281                 openl2tp_ppp_updown_ind(pppol2tp_tunnel_id,
282                                         pppol2tp_session_id, 1);
283         }
284 }
285
286 /*****************************************************************************
287  * Application init
288  *****************************************************************************/
289
290 void plugin_init(void)
291 {
292         old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
293         pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
294
295         old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
296         pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
297
298         old_multilink_join_hook = multilink_join_hook;
299         multilink_join_hook = openl2tp_multilink_join_ind;
300 }
301