1 /*****************************************************************************
2 * Copyright (C) 2006,2007,2008 Katalix Systems Ltd
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.
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.
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
18 *****************************************************************************/
20 /* pppd plugin for interfacing to openl2tpd */
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <linux/version.h>
36 #include <linux/sockios.h>
41 #include <pppd/pppd.h>
42 #include <pppd/options.h>
46 #include <pppd/ipcp.h>
47 #include <pppd/multilink.h>
51 /* should be defined in sys/types.h */
52 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
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>
61 #include "l2tp_event.h"
63 extern int pppol2tp_tunnel_id;
64 extern int pppol2tp_session_id;
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);
70 const char pppd_version[] = PPPD_VERSION;
72 static int openl2tp_fd = -1;
74 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
76 uint32_t recv_accm) = NULL;
77 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
79 #ifdef PPP_WITH_MULTILINK
80 static multilink_join_hook_fn *old_multilink_join_hook = NULL;
83 /*****************************************************************************
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
88 *****************************************************************************/
90 static int openl2tp_client_create(void)
92 struct sockaddr_un addr;
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");
102 addr.sun_family = AF_UNIX;
103 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
105 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
108 error("openl2tp connection connect: %m");
116 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
117 uint32_t send_accm, uint32_t recv_accm)
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;
127 if (openl2tp_fd < 0) {
128 result = openl2tp_client_create();
134 accm.send_accm = send_accm;
135 accm.recv_accm = recv_accm;
137 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
138 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
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);
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);
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);
159 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
162 error("openl2tp send: %m");
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);
168 dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
171 if (old_pppol2tp_send_accm_hook != NULL) {
172 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
173 send_accm, recv_accm);
178 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
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;
188 char ifname[MAXNAMELEN];
189 char user_name[MAXNAMELEN];
192 ppp_get_ifname(ifname, sizeof(ifname));
194 if (openl2tp_fd < 0) {
195 result = openl2tp_client_create();
201 if (!ppp_peer_authname(user_name, sizeof(user_name)))
204 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
205 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
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);
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);
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);
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);
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);
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);
246 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
249 error("openl2tp send: %m");
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);
255 dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
258 if (old_pppol2tp_ip_updown_hook != NULL) {
259 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
265 /*****************************************************************************
266 * When a multilink interface is created, there are 2 cases to consider.
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.
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.
279 * We use the ip_multilink_join_hook to hear when an interface joins a
281 *****************************************************************************/
283 #ifdef PPP_WITH_MULTILINK
284 static void openl2tp_multilink_join_ind(void)
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);
294 /*****************************************************************************
296 *****************************************************************************/
298 void plugin_init(void)
300 old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
301 pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
303 old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
304 pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
306 #ifdef PPP_WITH_MULTILINK
307 old_multilink_join_hook = multilink_join_hook;
308 multilink_join_hook = openl2tp_multilink_join_ind;