25 #include <linux/module.h>
26 #include <linux/kernel.h>
29 #include <linux/list.h>
31 #include <linux/netdevice.h>
32 #include <linux/poll.h>
33 #include <linux/ppp_defs.h>
34 #include <linux/filter.h>
37 #include <linux/ppp-comp.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/if_arp.h>
42 #include <linux/tcp.h>
45 #include <linux/stddef.h>
46 #include <linux/device.h>
48 #include <linux/slab.h>
49 #include <asm/unaligned.h>
57 #define PPP_VERSION "2.4.2"
71 #define MPHDRLEN_SSN 4
91 #define PF_TO_X(pf, X) container_of(pf, X, file)
93 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
94 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
137 #ifdef CONFIG_PPP_MULTILINK
145 #ifdef CONFIG_PPP_FILTER
148 unsigned pass_len, active_len;
161 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
162 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
163 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
179 #ifdef CONFIG_PPP_MULTILINK
227 #define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
230 #define PPP_MAX_RQLEN 32
238 #define PPP_MP_MAX_QLEN 128
245 #define seq_before(a, b) ((s32)((a) - (b)) < 0)
246 #define seq_after(a, b) ((s32)((a) - (b)) > 0)
251 static void ppp_xmit_process(
struct ppp *
ppp);
253 static void ppp_push(
struct ppp *
ppp);
254 static void ppp_channel_push(
struct channel *pch);
257 static void ppp_receive_error(
struct ppp *
ppp);
261 #ifdef CONFIG_PPP_MULTILINK
268 static int ppp_set_compress(
struct ppp *
ppp,
unsigned long arg);
269 static void ppp_ccp_peek(
struct ppp *
ppp,
struct sk_buff *
skb,
int inbound);
270 static void ppp_ccp_closed(
struct ppp *
ppp);
273 static struct ppp *ppp_create_interface(
struct net *
net,
int unit,
int *retp);
275 static void ppp_shutdown_interface(
struct ppp *
ppp);
276 static void ppp_destroy_interface(
struct ppp *
ppp);
279 static int ppp_connect_channel(
struct channel *pch,
int unit);
280 static int ppp_disconnect_channel(
struct channel *pch);
281 static void ppp_destroy_channel(
struct channel *pch);
282 static int unit_get(
struct idr *
p,
void *
ptr);
283 static int unit_set(
struct idr *
p,
void *
ptr,
int n);
284 static void unit_put(
struct idr *
p,
int n);
285 static void *unit_find(
struct idr *
p,
int n);
287 static struct class *ppp_class;
298 static inline int proto_to_npindex(
int proto)
318 static const int npindex_to_proto[
NUM_NP] = {
328 static inline int ethertype_to_npindex(
int ethertype)
349 static const int npindex_to_ethertype[
NUM_NP] = {
361 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
362 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
363 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
364 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
365 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
366 ppp_recv_lock(ppp); } while (0)
367 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
368 ppp_xmit_unlock(ppp); } while (0)
394 if (pf->
kind == INTERFACE) {
396 if (file == ppp->
owner)
397 ppp_shutdown_interface(ppp);
413 static ssize_t ppp_read(
struct file *file,
char __user *
buf,
414 size_t count, loff_t *ppos)
435 if (pf->
kind == INTERFACE) {
461 if (skb->
len > count)
476 static ssize_t ppp_write(
struct file *file,
const char __user *buf,
477 size_t count, loff_t *ppos)
489 skb_reserve(skb, pf->
hdrlen);
521 poll_wait(file, &pf->
rwait, wait);
523 if (skb_peek(&pf->
rq))
538 #ifdef CONFIG_PPP_FILTER
556 return PTR_ERR(code);
569 static long ppp_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
582 return ppp_unattached_ioctl(
current->nsproxy->net_ns,
599 if (pf->
kind == INTERFACE) {
601 if (file == ppp->
owner)
602 ppp_shutdown_interface(ppp);
604 if (atomic_long_read(&file->
f_count) <= 2) {
605 ppp_release(
NULL, file);
608 pr_warn(
"PPPIOCDETACH file->f_count=%ld\n",
609 atomic_long_read(&file->
f_count));
625 err = ppp_connect_channel(pch, unit);
629 err = ppp_disconnect_channel(pch);
636 if (chan && chan->
ops->ioctl)
637 err = chan->
ops->ioctl(chan, cmd, arg);
644 if (pf->
kind != INTERFACE) {
646 pr_err(
"PPP: not interface or channel??\n");
664 cflags = ppp->
flags & ~val;
680 err = ppp_set_compress(ppp, arg);
714 if ((val >> 16) != 0) {
721 "PPP: no memory (VJ compressor)\n");
737 err = proto_to_npindex(npi.protocol);
749 netif_wake_queue(ppp->
dev);
754 #ifdef CONFIG_PPP_FILTER
758 err = get_filter(argp, &code);
761 kfree(ppp->pass_filter);
762 ppp->pass_filter =
code;
772 err = get_filter(argp, &code);
775 kfree(ppp->active_filter);
776 ppp->active_filter =
code;
777 ppp->active_len =
err;
785 #ifdef CONFIG_PPP_MULTILINK
803 static int ppp_unattached_ioctl(
struct net *
net,
struct ppp_file *pf,
804 struct file *file,
unsigned int cmd,
unsigned long arg)
818 ppp = ppp_create_interface(net, unit, &err);
834 pn = ppp_pernet(net);
836 ppp = ppp_find_unit(pn, unit);
849 pn = ppp_pernet(net);
851 chan = ppp_find_channel(pn, unit);
872 .unlocked_ioctl = ppp_ioctl,
874 .release = ppp_release,
878 static __net_init int ppp_init_net(
struct net *net)
893 static __net_exit void ppp_exit_net(
struct net *net)
901 .init = ppp_init_net,
902 .exit = ppp_exit_net,
904 .size =
sizeof(
struct ppp_net),
907 #define PPP_MAJOR 108
911 static int __init ppp_init(
void)
919 pr_err(
"failed to register PPP pernet device (%d)\n", err);
923 err = register_chrdev(
PPP_MAJOR,
"ppp", &ppp_device_fops);
925 pr_err(
"failed to register PPP device (%d)\n", err);
930 if (IS_ERR(ppp_class)) {
931 err = PTR_ERR(ppp_class);
954 struct ppp *ppp = netdev_priv(dev);
963 switch (ppp->
npmode[npi]) {
981 proto = npindex_to_proto[npi];
982 put_unaligned_be16(proto, pp);
985 ppp_xmit_process(ppp);
990 ++dev->
stats.tx_dropped;
997 struct ppp *ppp = netdev_priv(dev);
1006 ppp_get_stats(ppp, &
stats);
1013 memset(&cstats, 0,
sizeof(cstats));
1040 struct ppp *ppp = netdev_priv(dev);
1062 .ndo_start_xmit = ppp_start_xmit,
1063 .ndo_do_ioctl = ppp_net_ioctl,
1067 static void ppp_setup(
struct net_device *dev)
1089 ppp_xmit_process(
struct ppp *ppp)
1098 ppp_send_frame(ppp, skb);
1102 netif_wake_queue(ppp->
dev);
1104 netif_stop_queue(ppp->
dev);
1109 static inline struct sk_buff *
1110 pad_compress_skb(
struct ppp *ppp,
struct sk_buff *skb)
1114 int new_skb_size = ppp->
dev->mtu +
1115 ppp->
xcomp->comp_extra + ppp->
dev->hard_header_len;
1116 int compressor_skb_size = ppp->
dev->mtu +
1118 new_skb = alloc_skb(new_skb_size,
GFP_ATOMIC);
1121 netdev_err(ppp->
dev,
"PPP: no memory (comp pkt)\n");
1125 skb_reserve(new_skb,
1131 compressor_skb_size);
1137 }
else if (len == 0) {
1151 netdev_err(ppp->
dev,
"ppp: compressor dropped pkt\n");
1165 ppp_send_frame(
struct ppp *ppp,
struct sk_buff *skb)
1172 if (proto < 0x8000) {
1173 #ifdef CONFIG_PPP_FILTER
1178 if (ppp->pass_filter &&
1182 "PPP: outbound frame "
1188 if (!(ppp->active_filter &&
1206 new_skb = alloc_skb(skb->
len + ppp->
dev->hard_header_len - 2,
1209 netdev_err(ppp->
dev,
"PPP: no memory (VJ comp pkt)\n");
1212 skb_reserve(new_skb, ppp->
dev->hard_header_len - 2);
1215 new_skb->
data + 2, &cp,
1217 if (cp == skb->
data + 2) {
1223 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1226 cp[0] = skb->
data[2];
1238 ppp_ccp_peek(ppp, skb, 0);
1247 netdev_err(ppp->
dev,
1248 "ppp: compression required but "
1249 "down - pkt dropped.\n");
1252 skb = pad_compress_skb(ppp, skb);
1275 ++ppp->
dev->stats.tx_errors;
1283 ppp_push(
struct ppp *ppp)
1293 if (list_empty(list)) {
1305 spin_lock_bh(&pch->
downl);
1307 if (pch->
chan->ops->start_xmit(pch->
chan, skb))
1314 spin_unlock_bh(&pch->
downl);
1318 #ifdef CONFIG_PPP_MULTILINK
1321 if (!ppp_mp_explode(ppp, skb))
1329 #ifdef CONFIG_PPP_MULTILINK
1333 "compress protocol id in multilink fragments");
1339 static int ppp_mp_explode(
struct ppp *ppp,
struct sk_buff *skb)
1344 int navail, nfree, nzero;
1348 unsigned char *
p, *
q;
1368 pch->speed = pch->
chan->speed;
1373 if (skb_queue_empty(&pch->
file.xq) ||
1375 if (pch->speed == 0)
1378 totspeed += pch->speed;
1384 if (!pch->had_frag && i < ppp->nxchan)
1394 if (nfree == 0 || nfree < navail / 2)
1400 if (*p == 0 && mp_protocol_compress) {
1406 nbigger = len % nfree;
1411 for (i = 0; i < ppp->nxchan; ++
i) {
1436 if (pch->avail == 1) {
1444 spin_lock_bh(&pch->
downl);
1447 if (pch->speed == 0)
1450 totspeed -= pch->speed;
1452 spin_unlock_bh(&pch->
downl);
1470 if (pch->speed == 0) {
1477 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1478 ((totspeed*totfree)/pch->speed)) -
hdrlen;
1480 flen += ((totfree - nzero)*pch->speed)/totspeed;
1481 nbigger -= ((totfree - nzero)*pch->speed)/
1493 if ((nfree <= 0) || (flen > len))
1503 spin_unlock_bh(&pch->
downl);
1512 mtu = pch->
chan->mtu - (hdrlen - 2);
1519 frag = alloc_skb(flen + hdrlen + (flen == 0),
GFP_ATOMIC);
1522 q =
skb_put(frag, flen + hdrlen);
1525 put_unaligned_be16(
PPP_MP, q);
1527 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1531 q[3] = ppp->nxseq >> 16;
1532 q[4] = ppp->nxseq >> 8;
1536 memcpy(q + hdrlen, p, flen);
1540 if (!skb_queue_empty(&pch->
file.xq) ||
1541 !chan->
ops->start_xmit(chan, frag))
1548 spin_unlock_bh(&pch->
downl);
1555 spin_unlock_bh(&pch->
downl);
1557 netdev_err(ppp->
dev,
"PPP: no memory (fragment)\n");
1558 ++ppp->
dev->stats.tx_errors;
1568 ppp_channel_push(
struct channel *pch)
1573 spin_lock_bh(&pch->
downl);
1575 while (!skb_queue_empty(&pch->
file.xq)) {
1577 if (!pch->
chan->ops->start_xmit(pch->
chan, skb)) {
1587 spin_unlock_bh(&pch->
downl);
1589 if (skb_queue_empty(&pch->
file.xq)) {
1593 ppp_xmit_process(ppp);
1606 #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
1609 ppp_do_recv(
struct ppp *ppp,
struct sk_buff *skb,
struct channel *pch)
1613 ppp_receive_frame(ppp, skb, pch);
1631 if (!pskb_may_pull(skb, 2)) {
1634 ++pch->
ppp->dev->stats.rx_length_errors;
1635 ppp_receive_error(pch->
ppp);
1650 ppp_do_recv(pch->
ppp, skb, pch);
1673 ppp_do_recv(pch->
ppp, skb, pch);
1684 ppp_receive_frame(
struct ppp *ppp,
struct sk_buff *skb,
struct channel *pch)
1688 #ifdef CONFIG_PPP_MULTILINK
1691 ppp_receive_mp_frame(ppp, skb, pch);
1694 ppp_receive_nonmp_frame(ppp, skb);
1697 ppp_receive_error(ppp);
1702 ppp_receive_error(
struct ppp *ppp)
1704 ++ppp->
dev->stats.rx_errors;
1710 ppp_receive_nonmp_frame(
struct ppp *ppp,
struct sk_buff *skb)
1722 skb = ppp_decompress_frame(ppp, skb);
1734 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
1736 ns = dev_alloc_skb(skb->
len + 128);
1738 netdev_err(ppp->
dev,
"PPP: no memory "
1753 "PPP: VJ decompression error\n");
1759 else if (len < skb->len)
1771 if (!pskb_may_pull(skb, skb->
len))
1775 netdev_err(ppp->
dev,
"PPP: VJ uncompressed error\n");
1782 ppp_ccp_peek(ppp, skb, 1);
1789 npi = proto_to_npindex(proto);
1803 #ifdef CONFIG_PPP_FILTER
1807 if (ppp->pass_filter || ppp->active_filter) {
1808 if (skb_cloned(skb) &&
1813 if (ppp->pass_filter &&
1817 "PPP: inbound frame "
1822 if (!(ppp->active_filter &&
1838 skb_reset_mac_header(skb);
1846 ppp_receive_error(ppp);
1850 ppp_decompress_frame(
struct ppp *ppp,
struct sk_buff *skb)
1859 if (!pskb_may_pull(skb, skb->
len))
1865 switch(ppp->
rcomp->compress_proto) {
1874 ns = dev_alloc_skb(obuff_size);
1876 netdev_err(ppp->
dev,
"ppp_decompress_frame: "
1882 skb->
len + 2, ns->
data, obuff_size);
1900 if (ppp->
rcomp->incomp)
1909 ppp_receive_error(ppp);
1913 #ifdef CONFIG_PPP_MULTILINK
1920 ppp_receive_mp_frame(
struct ppp *ppp,
struct sk_buff *skb,
struct channel *pch)
1926 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
1931 seq = ((skb->
data[2] & 0x0f) << 8) | skb->
data[3];
1934 seq = (skb->
data[3] << 16) | (skb->
data[4] << 8)| skb->
data[5];
1950 seq |= ppp->minseq & ~mask;
1951 if ((
int)(ppp->minseq - seq) > (
int)(mask >> 1))
1953 else if ((
int)(seq - ppp->minseq) > (
int)(mask >> 1))
1964 ++ppp->
dev->stats.rx_dropped;
1965 ppp_receive_error(ppp);
1985 ppp_mp_insert(ppp, skb);
1990 struct sk_buff *mskb = skb_peek(&ppp->mrq);
1992 ppp->minseq =
PPP_MP_CB(mskb)->sequence;
1996 while ((skb = ppp_mp_reconstruct(ppp))) {
1997 if (pskb_may_pull(skb, 2))
1998 ppp_receive_nonmp_frame(ppp, skb);
2000 ++ppp->
dev->stats.rx_length_errors;
2002 ppp_receive_error(ppp);
2010 ppp_receive_error(ppp);
2018 ppp_mp_insert(
struct ppp *ppp,
struct sk_buff *skb)
2026 skb_queue_walk(list, p) {
2030 __skb_queue_before(list, p, skb);
2040 ppp_mp_reconstruct(
struct ppp *ppp)
2042 u32 seq = ppp->nextseq;
2043 u32 minseq = ppp->minseq;
2048 int lost = 0, len = 0;
2054 skb_queue_walk_safe(list, p, tmp) {
2058 netdev_err(ppp->
dev,
"ppp_mp_reconstruct bad "
2061 __skb_unlink(p, list);
2079 "lost frag %u..%u\n",
2103 if (lost == 0 && (
PPP_MP_CB(p)->BEbits &
E) &&
2105 if (len > ppp->mrru + 2) {
2106 ++ppp->
dev->stats.rx_length_errors;
2108 "PPP: reconstructed packet"
2109 " is too long (%d)\n", len);
2114 ppp->nextseq = seq + 1;
2125 skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2128 "discarding frag %u\n",
2130 __skb_unlink(p, list);
2133 head = skb_peek(list);
2145 skb_queue_walk_safe(list, p, tmp) {
2150 "discarding frag %u\n",
2152 __skb_unlink(p, list);
2158 " missed pkts %u..%u\n",
2161 ++ppp->
dev->stats.rx_dropped;
2162 ppp_receive_error(ppp);
2167 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2168 p = skb_queue_next(list, head);
2169 __skb_unlink(skb, list);
2170 skb_queue_walk_from_safe(list, p, tmp) {
2171 __skb_unlink(p, list);
2184 __skb_unlink(skb, list);
2187 ppp->nextseq =
PPP_MP_CB(tail)->sequence + 1;
2214 pn = ppp_pernet(net);
2222 #ifdef CONFIG_PPP_MULTILINK
2246 return pch->
file.index;
2261 unit = pch->
ppp->file.index;
2277 if (pch->
ppp && pch->
ppp->dev)
2278 name = pch->
ppp->dev->name;
2305 spin_lock_bh(&pch->
downl);
2307 spin_unlock_bh(&pch->
downl);
2309 ppp_disconnect_channel(pch);
2319 ppp_destroy_channel(pch);
2333 ppp_channel_push(pch);
2342 ppp_set_compress(
struct ppp *ppp,
unsigned long arg)
2347 void *
state, *ostate;
2357 ccp_option[1] < 2 || ccp_option[1] >
data.length)
2361 find_compressor(ccp_option[0]),
2362 "ppp-compress-%d", ccp_option[0]);
2367 if (
data.transmit) {
2379 module_put(ocomp->
owner);
2383 module_put(cp->
owner);
2397 module_put(ocomp->
owner);
2401 module_put(cp->
owner);
2413 ppp_ccp_peek(
struct ppp *ppp,
struct sk_buff *skb,
int inbound)
2454 if (!pskb_may_pull(skb, len + 2))
2498 ppp_ccp_closed(
struct ppp *ppp)
2500 void *xstate, *rstate;
2517 module_put(xcomp->
owner);
2521 module_put(rcomp->
owner);
2535 find_comp_entry(
int proto)
2540 if (ce->
comp->compress_proto == proto)
2552 spin_lock(&compressor_list_lock);
2562 list_add(&ce->
list, &compressor_list);
2564 spin_unlock(&compressor_list_lock);
2574 spin_lock(&compressor_list_lock);
2576 if (ce && ce->
comp == cp) {
2580 spin_unlock(&compressor_list_lock);
2585 find_compressor(
int type)
2590 spin_lock(&compressor_list_lock);
2591 ce = find_comp_entry(type);
2594 if (!try_module_get(cp->
owner))
2597 spin_unlock(&compressor_list_lock);
2606 ppp_get_stats(
struct ppp *ppp,
struct ppp_stats *
st)
2610 memset(st, 0,
sizeof(*st));
2611 st->
p.ppp_ipackets = ppp->
stats64.rx_packets;
2612 st->
p.ppp_ierrors = ppp->
dev->stats.rx_errors;
2613 st->
p.ppp_ibytes = ppp->
stats64.rx_bytes;
2614 st->
p.ppp_opackets = ppp->
stats64.tx_packets;
2615 st->
p.ppp_oerrors = ppp->
dev->stats.tx_errors;
2616 st->
p.ppp_obytes = ppp->
stats64.tx_bytes;
2640 ppp_create_interface(
struct net *net,
int unit,
int *retp)
2652 pn = ppp_pernet(net);
2654 ppp = netdev_priv(dev);
2657 init_ppp_file(&ppp->
file, INTERFACE);
2664 #ifdef CONFIG_PPP_MULTILINK
2666 skb_queue_head_init(&ppp->mrq);
2673 dev_net_set(dev, net);
2696 unit = unit_set(&pn->
units_idr, ppp, unit);
2708 netdev_err(ppp->
dev,
"PPP: couldn't register device %s (%d)\n",
2736 skb_queue_head_init(&pf->
xq);
2737 skb_queue_head_init(&pf->
rq);
2746 static void ppp_shutdown_interface(
struct ppp *ppp)
2750 pn = ppp_pernet(ppp->
ppp_net);
2775 static void ppp_destroy_interface(
struct ppp *ppp)
2781 netdev_err(ppp->
dev,
"ppp: destroying ppp struct %p "
2782 "but dead=%d n_channels=%d !\n",
2787 ppp_ccp_closed(ppp);
2794 #ifdef CONFIG_PPP_MULTILINK
2797 #ifdef CONFIG_PPP_FILTER
2798 kfree(ppp->pass_filter);
2799 ppp->pass_filter =
NULL;
2800 kfree(ppp->active_filter);
2801 ppp->active_filter =
NULL;
2814 ppp_find_unit(
struct ppp_net *pn,
int unit)
2828 ppp_find_channel(
struct ppp_net *pn,
int unit)
2833 if (pch->
file.index == unit) {
2840 if (pch->
file.index == unit)
2851 ppp_connect_channel(
struct channel *pch,
int unit)
2861 ppp = ppp_find_unit(pn, unit);
2870 if (pch->
file.hdrlen > ppp->
file.hdrlen)
2871 ppp->
file.hdrlen = pch->
file.hdrlen;
2872 hdrlen = pch->
file.hdrlen + 2;
2873 if (hdrlen > ppp->
dev->hard_header_len)
2893 ppp_disconnect_channel(
struct channel *pch)
2910 ppp_destroy_interface(ppp);
2919 static void ppp_destroy_channel(
struct channel *pch)
2923 if (!pch->
file.dead) {
2925 pr_err(
"ppp: destroying undead channel %p !\n", pch);
2933 static void __exit ppp_cleanup(
void)
2937 pr_err(
"PPP: removing module but units remain!\n");
2949 static int __unit_alloc(
struct idr *p,
void *
ptr,
int n)
2955 pr_err(
"PPP: No free memory for idr\n");
2970 static int unit_set(
struct idr *p,
void *ptr,
int n)
2974 unit = __unit_alloc(p, ptr, n);
2977 else if (unit != n) {
2986 static int unit_get(
struct idr *p,
void *ptr)
2988 return __unit_alloc(p, ptr, 0);
2992 static void unit_put(
struct idr *p,
int n)
2998 static void *unit_find(
struct idr *p,
int n)