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 */
27 #include "pathnames.h"
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include <linux/version.h>
40 #include <linux/sockios.h>
43 /* should be defined in sys/types.h */
44 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
46 #include <linux/types.h>
47 #include <linux/if_ether.h>
48 #include <linux/ppp_defs.h>
49 #include <linux/if_ppp.h>
50 #include <linux/if_pppox.h>
51 #include <linux/if_pppol2tp.h>
53 #include "l2tp_event.h"
55 extern int pppol2tp_tunnel_id;
56 extern int pppol2tp_session_id;
58 extern void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
59 uint32_t send_accm, uint32_t recv_accm);
60 extern void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up);
62 const char pppd_version[] = VERSION;
64 static int openl2tp_fd = -1;
66 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
68 uint32_t recv_accm) = NULL;
69 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
71 static void (*old_multilink_join_hook)(void) = NULL;
73 /*****************************************************************************
75 * We send a PPP_ACCM_IND to openl2tpd to report ACCM values and
76 * SESSION_PPP_UPDOWN_IND to indicate when the PPP link comes up or
78 *****************************************************************************/
80 static int openl2tp_client_create(void)
82 struct sockaddr_un addr;
85 if (openl2tp_fd < 0) {
86 openl2tp_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
87 if (openl2tp_fd < 0) {
88 error("openl2tp connection create: %m");
92 addr.sun_family = AF_UNIX;
93 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
95 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
98 error("openl2tp connection connect: %m");
106 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
107 uint32_t send_accm, uint32_t recv_accm)
110 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
111 struct openl2tp_event_msg *msg = (void *) &buf[0];
112 struct openl2tp_event_tlv *tlv;
113 uint16_t tid = tunnel_id;
114 uint16_t sid = session_id;
115 struct openl2tp_tlv_ppp_accm accm;
117 if (openl2tp_fd < 0) {
118 result = openl2tp_client_create();
124 accm.send_accm = send_accm;
125 accm.recv_accm = recv_accm;
127 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
128 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
131 tlv = (void *) &msg->msg_data[msg->msg_len];
132 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
133 tlv->tlv_len = sizeof(tid);
134 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
135 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
137 tlv = (void *) &msg->msg_data[msg->msg_len];
138 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
139 tlv->tlv_len = sizeof(sid);
140 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
141 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
143 tlv = (void *) &msg->msg_data[msg->msg_len];
144 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_ACCM;
145 tlv->tlv_len = sizeof(accm);
146 memcpy(&tlv->tlv_value[0], &accm, tlv->tlv_len);
147 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
149 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
152 error("openl2tp send: %m");
154 if (result != (sizeof(*msg) + msg->msg_len)) {
155 warn("openl2tp send: unexpected byte count %d, expected %d",
156 result, sizeof(msg) + msg->msg_len);
158 dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
161 if (old_pppol2tp_send_accm_hook != NULL) {
162 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
163 send_accm, recv_accm);
168 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
171 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
172 struct openl2tp_event_msg *msg = (void *) &buf[0];
173 struct openl2tp_event_tlv *tlv;
174 uint16_t tid = tunnel_id;
175 uint16_t sid = session_id;
178 char *user_name = NULL;
180 if (openl2tp_fd < 0) {
181 result = openl2tp_client_create();
187 if (peer_authname[0] != '\0') {
188 user_name = strdup(peer_authname);
191 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
192 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
195 tlv = (void *) &msg->msg_data[msg->msg_len];
196 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
197 tlv->tlv_len = sizeof(tid);
198 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
199 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
201 tlv = (void *) &msg->msg_data[msg->msg_len];
202 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
203 tlv->tlv_len = sizeof(sid);
204 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
205 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
207 tlv = (void *) &msg->msg_data[msg->msg_len];
208 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_STATE;
209 tlv->tlv_len = sizeof(state);
210 memcpy(&tlv->tlv_value[0], &state, tlv->tlv_len);
211 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
213 tlv = (void *) &msg->msg_data[msg->msg_len];
214 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_UNIT;
215 tlv->tlv_len = sizeof(unit);
216 memcpy(&tlv->tlv_value[0], &unit, tlv->tlv_len);
217 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
219 tlv = (void *) &msg->msg_data[msg->msg_len];
220 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_IFNAME;
221 tlv->tlv_len = strlen(ifname) + 1;
222 memcpy(&tlv->tlv_value[0], ifname, tlv->tlv_len);
223 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
225 if (user_name != NULL) {
226 tlv = (void *) &msg->msg_data[msg->msg_len];
227 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_USER_NAME;
228 tlv->tlv_len = strlen(user_name) + 1;
229 memcpy(&tlv->tlv_value[0], user_name, tlv->tlv_len);
230 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
233 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
236 error("openl2tp send: %m");
238 if (result != (sizeof(*msg) + msg->msg_len)) {
239 warn("openl2tp send: unexpected byte count %d, expected %d",
240 result, sizeof(msg) + msg->msg_len);
242 dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
245 if (old_pppol2tp_ip_updown_hook != NULL) {
246 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
252 /*****************************************************************************
253 * When a multilink interface is created, there are 2 cases to consider.
255 * 1. The new interface is the first of a multilink bundle (master).
256 * 2. The new interface is being attached to an existing bundle.
258 * The first case is handled by existing code because the interface
259 * generates ip-up events just like standard interfaces. But in the
260 * second case, where the interface is added to an existing ppp
261 * bundle, pppd does not do IP negotiation and so as a result, no
262 * ip-up event is generated when the interface is created. Since
263 * openl2tpd needs the SESSION_PPP_UPDOWN_IND for all interfaces of a
264 * PPP bundle, we must fake the event.
266 * We use the ip_multilink_join_hook to hear when an interface joins a
268 *****************************************************************************/
270 static void openl2tp_multilink_join_ind(void)
272 if (doing_multilink && !multilink_master) {
273 /* send event only if not master */
274 openl2tp_ppp_updown_ind(pppol2tp_tunnel_id,
275 pppol2tp_session_id, 1);
279 /*****************************************************************************
281 *****************************************************************************/
283 void plugin_init(void)
285 old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
286 pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
288 old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
289 pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
291 old_multilink_join_hook = multilink_join_hook;
292 multilink_join_hook = openl2tp_multilink_join_ind;