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