1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <sys/systm.h>
25#include <sys/mbuf.h>
26#include <sys/socket.h>
27#include <sys/syslog.h>
28#include <sys/protosw.h>
29#include <kern/locks.h>
30
31#include <net/if_types.h>
32#include <net/dlil.h>
33#include <netinet/in.h>
34#include <netinet/in_systm.h>
35#include <netinet/in_var.h>
36#include <netinet/ip_var.h>
37#include <netinet/ip.h>
38
39
40#include "../../../Family/ppp_domain.h"
41#include "pptp_rfc.h"
42#include "pptp_ip.h"
43
44
45/* -----------------------------------------------------------------------------
46Definitions
47----------------------------------------------------------------------------- */
48
49
50/* -----------------------------------------------------------------------------
51Declarations
52----------------------------------------------------------------------------- */
53
54mbuf_t 	pptp_ip_input(mbuf_t , int len, int other);
55
56/* -----------------------------------------------------------------------------
57Globals
58----------------------------------------------------------------------------- */
59
60extern lck_mtx_t	*ppp_domain_mutex;
61
62/* -----------------------------------------------------------------------------
63intialize pptp datalink attachment strutures
64----------------------------------------------------------------------------- */
65int pptp_ip_init()
66{
67	ip_gre_register_input((gre_input_func_t)pptp_ip_input);
68	return 0;
69}
70
71/* -----------------------------------------------------------------------------
72dispose pptp datalink structures
73can't dispose if clients are still attached
74----------------------------------------------------------------------------- */
75int pptp_ip_dispose()
76{
77	ip_gre_register_input(NULL);
78	return 0;
79}
80
81/* -----------------------------------------------------------------------------
82callback from ip
83----------------------------------------------------------------------------- */
84mbuf_t pptp_ip_input(mbuf_t m, int len, int other)
85{
86    struct ip 		*ip, ip_data;
87    u_int32_t 		from;
88	int				success;
89
90#if 0
91    u_int8_t 		*d, i;
92    d = mtod(m, u_int8_t *);
93    for (i = 0; i < 64; i+=16) {
94    IOLog("pptp_ip_input: data 0x %x %x %x %x %x %x %x %x - %x %x %x %x %x %x %x %x\n",
95        d[i+0],d[i+1],d[i+2],d[i+3],d[i+4],d[i+5], d[i+6], d[i+7],
96        d[i+8], d[i+9], d[i+10], d[i+11], d[i+12], d[i+13], d[i+14], d[i+15]);
97    }
98#endif
99
100    ip = &ip_data;
101    memcpy(ip, mbuf_data(m), sizeof(ip_data));
102    from = ip->ip_src.s_addr;
103
104    /* remove the IP header */
105    mbuf_adj(m, ip->ip_hl * 4);
106
107	lck_mtx_lock(ppp_domain_mutex);
108    success = pptp_rfc_lower_input(m, from);
109	lck_mtx_unlock(ppp_domain_mutex);
110	if (success)
111        return NULL;
112
113	return m;
114}
115
116/* -----------------------------------------------------------------------------
117called from pppenet_proto when data need to be sent
118----------------------------------------------------------------------------- */
119int pptp_ip_output(mbuf_t m, u_int32_t from, u_int32_t to)
120{
121    struct ip 	*ip, ip_data;
122
123#if 0
124    u_int8_t 	*d, i;
125
126    d = mtod(m, u_int8_t *);
127    for (i = 0; i < 64; i+=16) {
128    IOLog("pptp_ip_output: data 0x %x %x %x %x %x %x %x %x - %x %x %x %x %x %x %x %x\n",
129        d[i+0],d[i+1],d[i+2],d[i+3],d[i+4],d[i+5], d[i+6], d[i+7],
130        d[i+8], d[i+9], d[i+10], d[i+11], d[i+12], d[i+13], d[i+14], d[i+15]);
131    }
132#endif
133
134    if (mbuf_prepend(&m, sizeof(struct ip), MBUF_WAITOK) != 0)
135        return 1;
136
137    ip = &ip_data;
138    memcpy(ip, mbuf_data(m), sizeof(ip_data));
139    ip->ip_tos = 0;
140    ip->ip_off = 0;
141    ip->ip_p = IPPROTO_GRE;
142    ip->ip_len = mbuf_pkthdr_len(m);
143    ip->ip_src.s_addr = from;
144    ip->ip_dst.s_addr = to;
145    ip->ip_ttl = MAXTTL;
146    memcpy(mbuf_data(m), ip, sizeof(ip_data));
147
148	lck_mtx_unlock(ppp_domain_mutex);
149    ip_gre_output((struct mbuf *)m);
150	lck_mtx_lock(ppp_domain_mutex);
151
152    return 0;
153}
154
155