]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppol2tp/openl2tp.c
940d2bc4e59e4682880b5badb0170c8c7f4c59ff
[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 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26
27 #include <sys/stat.h>
28 #include <net/if.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <sys/time.h>
33 #include <netinet/in.h>
34 #include <signal.h>
35 #include <linux/version.h>
36 #include <linux/sockios.h>
37 #include <stdarg.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40
41 #include <pppd/pppd.h>
42 #include <pppd/options.h>
43 #include <pppd/fsm.h>
44 #include <pppd/lcp.h>
45 #include <pppd/ccp.h>
46 #include <pppd/ipcp.h>
47 #include <pppd/multilink.h>
48
49
50 #ifndef aligned_u64
51 /* should be defined in sys/types.h */
52 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
53 #endif
54 #include <linux/types.h>
55 #include <linux/if_ether.h>
56 #include <linux/ppp_defs.h>
57 #include <linux/if_ppp.h>
58 #include <linux/if_pppox.h>
59 #include <linux/if_pppol2tp.h>
60
61 #include "l2tp_event.h"
62
63 extern int pppol2tp_tunnel_id;
64 extern int pppol2tp_session_id;
65
66 extern void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
67                                        uint32_t send_accm, uint32_t recv_accm);
68 extern void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up);
69
70 const char pppd_version[] = PPPD_VERSION;
71
72 static int openl2tp_fd = -1;
73
74 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
75                                            uint32_t send_accm,
76                                            uint32_t recv_accm) = NULL;
77 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
78                                            int up) = NULL;
79 #ifdef PPP_WITH_MULTILINK
80 static multilink_join_hook_fn *old_multilink_join_hook = NULL;
81 #endif
82
83 /*****************************************************************************
84  * OpenL2TP interface.
85  * We send a PPP_ACCM_IND to openl2tpd to report ACCM values and
86  * SESSION_PPP_UPDOWN_IND to indicate when the PPP link comes up or
87  * goes down.
88  *****************************************************************************/
89
90 static int openl2tp_client_create(void)
91 {
92         struct sockaddr_un addr;
93         int result;
94
95         if (openl2tp_fd < 0) {
96                 openl2tp_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
97                 if (openl2tp_fd < 0) {
98                         error("openl2tp connection create: %m");
99                         return -ENOTCONN;
100                 }
101
102                 addr.sun_family = AF_UNIX;
103                 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
104
105                 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
106                                  sizeof(addr));
107                 if (result < 0) {
108                         error("openl2tp connection connect: %m");
109                         return -ENOTCONN;
110                 }
111         }
112
113         return 0;
114 }
115
116 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
117                                    uint32_t send_accm, uint32_t recv_accm)
118 {
119         int result;
120         uint8_t buf[OPENL2TP_MSG_MAX_LEN];
121         struct openl2tp_event_msg *msg = (void *) &buf[0];
122         struct openl2tp_event_tlv *tlv;
123         uint16_t tid = tunnel_id;
124         uint16_t sid = session_id;
125         struct openl2tp_tlv_ppp_accm accm;
126
127         if (openl2tp_fd < 0) {
128                 result = openl2tp_client_create();
129                 if (result < 0) {
130                         goto out;
131                 }
132         }
133
134         accm.send_accm = send_accm;
135         accm.recv_accm = recv_accm;
136
137         msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
138         msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
139         msg->msg_len = 0;
140
141         tlv = (void *) &msg->msg_data[msg->msg_len];
142         tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
143         tlv->tlv_len = sizeof(tid);
144         memcpy(&tlv->tlv_value[0], &tid, 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_SESSION_ID;
149         tlv->tlv_len = sizeof(sid);
150         memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
151         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
152
153         tlv = (void *) &msg->msg_data[msg->msg_len];
154         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_ACCM;
155         tlv->tlv_len = sizeof(accm);
156         memcpy(&tlv->tlv_value[0], &accm, tlv->tlv_len);
157         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
158
159         result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
160                       MSG_NOSIGNAL);
161         if (result < 0) {
162                 error("openl2tp send: %m");
163         }
164         if (result != (sizeof(*msg) + msg->msg_len)) {
165                 warn("openl2tp send: unexpected byte count %d, expected %d",
166                      result, sizeof(msg) + msg->msg_len);
167         }
168         dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
169
170 out:
171         if (old_pppol2tp_send_accm_hook != NULL) {
172                 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
173                                                send_accm, recv_accm);
174         }
175         return;
176 }
177
178 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
179 {
180         int result;
181         uint8_t buf[OPENL2TP_MSG_MAX_LEN];
182         struct openl2tp_event_msg *msg = (void *) &buf[0];
183         struct openl2tp_event_tlv *tlv;
184         uint16_t tid = tunnel_id;
185         uint16_t sid = session_id;
186         uint8_t state = up;
187         int unit = 0;
188         char ifname[MAXNAMELEN];
189         char user_name[MAXNAMELEN];
190
191         unit = ppp_ifunit();
192         ppp_get_ifname(ifname, sizeof(ifname));
193
194         if (openl2tp_fd < 0) {
195                 result = openl2tp_client_create();
196                 if (result < 0) {
197                         goto out;
198                 }
199         }
200
201         if (!ppp_peer_authname(user_name, sizeof(user_name)))
202                 user_name[0] = '\0';
203
204         msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
205         msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
206         msg->msg_len = 0;
207
208         tlv = (void *) &msg->msg_data[msg->msg_len];
209         tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
210         tlv->tlv_len = sizeof(tid);
211         memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
212         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
213
214         tlv = (void *) &msg->msg_data[msg->msg_len];
215         tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
216         tlv->tlv_len = sizeof(sid);
217         memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
218         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
219
220         tlv = (void *) &msg->msg_data[msg->msg_len];
221         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_STATE;
222         tlv->tlv_len = sizeof(state);
223         memcpy(&tlv->tlv_value[0], &state, tlv->tlv_len);
224         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
225
226         tlv = (void *) &msg->msg_data[msg->msg_len];
227         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_UNIT;
228         tlv->tlv_len = sizeof(unit);
229         memcpy(&tlv->tlv_value[0], &unit, tlv->tlv_len);
230         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
231
232         tlv = (void *) &msg->msg_data[msg->msg_len];
233         tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_IFNAME;
234         tlv->tlv_len = strlen(ifname) + 1;
235         memcpy(&tlv->tlv_value[0], ifname, tlv->tlv_len);
236         msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
237
238         if (user_name[0] != '\0') {
239                 tlv = (void *) &msg->msg_data[msg->msg_len];
240                 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_USER_NAME;
241                 tlv->tlv_len = strlen(user_name) + 1;
242                 memcpy(&tlv->tlv_value[0], user_name, tlv->tlv_len);
243                 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
244         }
245
246         result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
247                       MSG_NOSIGNAL);
248         if (result < 0) {
249                 error("openl2tp send: %m");
250         }
251         if (result != (sizeof(*msg) + msg->msg_len)) {
252                 warn("openl2tp send: unexpected byte count %d, expected %d",
253                      result, sizeof(msg) + msg->msg_len);
254         }
255         dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
256
257 out:
258         if (old_pppol2tp_ip_updown_hook != NULL) {
259                 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
260         }
261
262         return;
263 }
264
265 /*****************************************************************************
266  * When a multilink interface is created, there are 2 cases to consider.
267  *
268  * 1. The new interface is the first of a multilink bundle (master).
269  * 2. The new interface is being attached to an existing bundle.
270  *
271  * The first case is handled by existing code because the interface
272  * generates ip-up events just like standard interfaces. But in the
273  * second case, where the interface is added to an existing ppp
274  * bundle, pppd does not do IP negotiation and so as a result, no
275  * ip-up event is generated when the interface is created. Since
276  * openl2tpd needs the SESSION_PPP_UPDOWN_IND for all interfaces of a
277  * PPP bundle, we must fake the event.
278  *
279  * We use the ip_multilink_join_hook to hear when an interface joins a
280  * multilink bundle.
281  *****************************************************************************/
282
283 #ifdef PPP_WITH_MULTILINK
284 static void openl2tp_multilink_join_ind(void)
285 {
286         if (mp_on() && !mp_master()) {
287                 /* send event only if not master */
288                 openl2tp_ppp_updown_ind(pppol2tp_tunnel_id,
289                                         pppol2tp_session_id, 1);
290         }
291 }
292 #endif
293
294 /*****************************************************************************
295  * Application init
296  *****************************************************************************/
297
298 void plugin_init(void)
299 {
300         old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
301         pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
302
303         old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
304         pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
305
306 #ifdef PPP_WITH_MULTILINK
307         old_multilink_join_hook = multilink_join_hook;
308         multilink_join_hook = openl2tp_multilink_join_ind;
309 #endif
310 }
311