ppp_generic.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746
  1. /*
  2. * Generic PPP layer for Linux.
  3. *
  4. * Copyright 1999-2002 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * The generic PPP layer handles the PPP network interfaces, the
  12. * /dev/ppp device, packet and VJ compression, and multilink.
  13. * It talks to PPP `channels' via the interface defined in
  14. * include/linux/ppp_channel.h. Channels provide the basic means for
  15. * sending and receiving PPP frames on some kind of communications
  16. * channel.
  17. *
  18. * Part of the code in this driver was inspired by the old async-only
  19. * PPP driver, written by Michael Callahan and Al Longyear, and
  20. * subsequently hacked by Paul Mackerras.
  21. *
  22. * ==FILEVERSION 20041108==
  23. */
  24. #include <linux/config.h>
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/kmod.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #include <linux/devfs_fs_kernel.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/poll.h>
  33. #include <linux/ppp_defs.h>
  34. #include <linux/filter.h>
  35. #include <linux/if_ppp.h>
  36. #include <linux/ppp_channel.h>
  37. #include <linux/ppp-comp.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/rtnetlink.h>
  40. #include <linux/if_arp.h>
  41. #include <linux/ip.h>
  42. #include <linux/tcp.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/rwsem.h>
  46. #include <linux/stddef.h>
  47. #include <linux/device.h>
  48. #include <net/slhc_vj.h>
  49. #include <asm/atomic.h>
  50. #define PPP_VERSION "2.4.2"
  51. /*
  52. * Network protocols we support.
  53. */
  54. #define NP_IP 0 /* Internet Protocol V4 */
  55. #define NP_IPV6 1 /* Internet Protocol V6 */
  56. #define NP_IPX 2 /* IPX protocol */
  57. #define NP_AT 3 /* Appletalk protocol */
  58. #define NP_MPLS_UC 4 /* MPLS unicast */
  59. #define NP_MPLS_MC 5 /* MPLS multicast */
  60. #define NUM_NP 6 /* Number of NPs. */
  61. #define MPHDRLEN 6 /* multilink protocol header length */
  62. #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
  63. #define MIN_FRAG_SIZE 64
  64. /*
  65. * An instance of /dev/ppp can be associated with either a ppp
  66. * interface unit or a ppp channel. In both cases, file->private_data
  67. * points to one of these.
  68. */
  69. struct ppp_file {
  70. enum {
  71. INTERFACE=1, CHANNEL
  72. } kind;
  73. struct sk_buff_head xq; /* pppd transmit queue */
  74. struct sk_buff_head rq; /* receive queue for pppd */
  75. wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
  76. atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
  77. int hdrlen; /* space to leave for headers */
  78. int index; /* interface unit / channel number */
  79. int dead; /* unit/channel has been shut down */
  80. };
  81. #define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
  82. #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
  83. #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
  84. #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
  85. /*
  86. * Data structure describing one ppp unit.
  87. * A ppp unit corresponds to a ppp network interface device
  88. * and represents a multilink bundle.
  89. * It can have 0 or more ppp channels connected to it.
  90. */
  91. struct ppp {
  92. struct ppp_file file; /* stuff for read/write/poll 0 */
  93. struct file *owner; /* file that owns this unit 48 */
  94. struct list_head channels; /* list of attached channels 4c */
  95. int n_channels; /* how many channels are attached 54 */
  96. spinlock_t rlock; /* lock for receive side 58 */
  97. spinlock_t wlock; /* lock for transmit side 5c */
  98. int mru; /* max receive unit 60 */
  99. unsigned int flags; /* control bits 64 */
  100. unsigned int xstate; /* transmit state bits 68 */
  101. unsigned int rstate; /* receive state bits 6c */
  102. int debug; /* debug flags 70 */
  103. struct slcompress *vj; /* state for VJ header compression */
  104. enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
  105. struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
  106. struct compressor *xcomp; /* transmit packet compressor 8c */
  107. void *xc_state; /* its internal state 90 */
  108. struct compressor *rcomp; /* receive decompressor 94 */
  109. void *rc_state; /* its internal state 98 */
  110. unsigned long last_xmit; /* jiffies when last pkt sent 9c */
  111. unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
  112. struct net_device *dev; /* network interface device a4 */
  113. #ifdef CONFIG_PPP_MULTILINK
  114. int nxchan; /* next channel to send something on */
  115. u32 nxseq; /* next sequence number to send */
  116. int mrru; /* MP: max reconst. receive unit */
  117. u32 nextseq; /* MP: seq no of next packet */
  118. u32 minseq; /* MP: min of most recent seqnos */
  119. struct sk_buff_head mrq; /* MP: receive reconstruction queue */
  120. #endif /* CONFIG_PPP_MULTILINK */
  121. struct net_device_stats stats; /* statistics */
  122. #ifdef CONFIG_PPP_FILTER
  123. struct sock_filter *pass_filter; /* filter for packets to pass */
  124. struct sock_filter *active_filter;/* filter for pkts to reset idle */
  125. unsigned pass_len, active_len;
  126. #endif /* CONFIG_PPP_FILTER */
  127. };
  128. /*
  129. * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
  130. * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
  131. * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
  132. * Bits in xstate: SC_COMP_RUN
  133. */
  134. #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
  135. |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
  136. |SC_COMP_TCP|SC_REJ_COMP_TCP)
  137. /*
  138. * Private data structure for each channel.
  139. * This includes the data structure used for multilink.
  140. */
  141. struct channel {
  142. struct ppp_file file; /* stuff for read/write/poll */
  143. struct list_head list; /* link in all/new_channels list */
  144. struct ppp_channel *chan; /* public channel data structure */
  145. struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
  146. spinlock_t downl; /* protects `chan', file.xq dequeue */
  147. struct ppp *ppp; /* ppp unit we're connected to */
  148. struct list_head clist; /* link in list of channels per unit */
  149. rwlock_t upl; /* protects `ppp' */
  150. #ifdef CONFIG_PPP_MULTILINK
  151. u8 avail; /* flag used in multilink stuff */
  152. u8 had_frag; /* >= 1 fragments have been sent */
  153. u32 lastseq; /* MP: last sequence # received */
  154. #endif /* CONFIG_PPP_MULTILINK */
  155. };
  156. /*
  157. * SMP locking issues:
  158. * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
  159. * list and the ppp.n_channels field, you need to take both locks
  160. * before you modify them.
  161. * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
  162. * channel.downl.
  163. */
  164. /*
  165. * A cardmap represents a mapping from unsigned integers to pointers,
  166. * and provides a fast "find lowest unused number" operation.
  167. * It uses a broad (32-way) tree with a bitmap at each level.
  168. * It is designed to be space-efficient for small numbers of entries
  169. * and time-efficient for large numbers of entries.
  170. */
  171. #define CARDMAP_ORDER 5
  172. #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
  173. #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
  174. struct cardmap {
  175. int shift;
  176. unsigned long inuse;
  177. struct cardmap *parent;
  178. void *ptr[CARDMAP_WIDTH];
  179. };
  180. static void *cardmap_get(struct cardmap *map, unsigned int nr);
  181. static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
  182. static unsigned int cardmap_find_first_free(struct cardmap *map);
  183. static void cardmap_destroy(struct cardmap **map);
  184. /*
  185. * all_ppp_sem protects the all_ppp_units mapping.
  186. * It also ensures that finding a ppp unit in the all_ppp_units map
  187. * and updating its file.refcnt field is atomic.
  188. */
  189. static DECLARE_MUTEX(all_ppp_sem);
  190. static struct cardmap *all_ppp_units;
  191. static atomic_t ppp_unit_count = ATOMIC_INIT(0);
  192. /*
  193. * all_channels_lock protects all_channels and last_channel_index,
  194. * and the atomicity of find a channel and updating its file.refcnt
  195. * field.
  196. */
  197. static DEFINE_SPINLOCK(all_channels_lock);
  198. static LIST_HEAD(all_channels);
  199. static LIST_HEAD(new_channels);
  200. static int last_channel_index;
  201. static atomic_t channel_count = ATOMIC_INIT(0);
  202. /* Get the PPP protocol number from a skb */
  203. #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
  204. /* We limit the length of ppp->file.rq to this (arbitrary) value */
  205. #define PPP_MAX_RQLEN 32
  206. /*
  207. * Maximum number of multilink fragments queued up.
  208. * This has to be large enough to cope with the maximum latency of
  209. * the slowest channel relative to the others. Strictly it should
  210. * depend on the number of channels and their characteristics.
  211. */
  212. #define PPP_MP_MAX_QLEN 128
  213. /* Multilink header bits. */
  214. #define B 0x80 /* this fragment begins a packet */
  215. #define E 0x40 /* this fragment ends a packet */
  216. /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
  217. #define seq_before(a, b) ((s32)((a) - (b)) < 0)
  218. #define seq_after(a, b) ((s32)((a) - (b)) > 0)
  219. /* Prototypes. */
  220. static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
  221. unsigned int cmd, unsigned long arg);
  222. static void ppp_xmit_process(struct ppp *ppp);
  223. static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
  224. static void ppp_push(struct ppp *ppp);
  225. static void ppp_channel_push(struct channel *pch);
  226. static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
  227. struct channel *pch);
  228. static void ppp_receive_error(struct ppp *ppp);
  229. static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
  230. static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
  231. struct sk_buff *skb);
  232. #ifdef CONFIG_PPP_MULTILINK
  233. static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
  234. struct channel *pch);
  235. static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
  236. static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
  237. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
  238. #endif /* CONFIG_PPP_MULTILINK */
  239. static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
  240. static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
  241. static void ppp_ccp_closed(struct ppp *ppp);
  242. static struct compressor *find_compressor(int type);
  243. static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
  244. static struct ppp *ppp_create_interface(int unit, int *retp);
  245. static void init_ppp_file(struct ppp_file *pf, int kind);
  246. static void ppp_shutdown_interface(struct ppp *ppp);
  247. static void ppp_destroy_interface(struct ppp *ppp);
  248. static struct ppp *ppp_find_unit(int unit);
  249. static struct channel *ppp_find_channel(int unit);
  250. static int ppp_connect_channel(struct channel *pch, int unit);
  251. static int ppp_disconnect_channel(struct channel *pch);
  252. static void ppp_destroy_channel(struct channel *pch);
  253. static struct class_simple *ppp_class;
  254. /* Translates a PPP protocol number to a NP index (NP == network protocol) */
  255. static inline int proto_to_npindex(int proto)
  256. {
  257. switch (proto) {
  258. case PPP_IP:
  259. return NP_IP;
  260. case PPP_IPV6:
  261. return NP_IPV6;
  262. case PPP_IPX:
  263. return NP_IPX;
  264. case PPP_AT:
  265. return NP_AT;
  266. case PPP_MPLS_UC:
  267. return NP_MPLS_UC;
  268. case PPP_MPLS_MC:
  269. return NP_MPLS_MC;
  270. }
  271. return -EINVAL;
  272. }
  273. /* Translates an NP index into a PPP protocol number */
  274. static const int npindex_to_proto[NUM_NP] = {
  275. PPP_IP,
  276. PPP_IPV6,
  277. PPP_IPX,
  278. PPP_AT,
  279. PPP_MPLS_UC,
  280. PPP_MPLS_MC,
  281. };
  282. /* Translates an ethertype into an NP index */
  283. static inline int ethertype_to_npindex(int ethertype)
  284. {
  285. switch (ethertype) {
  286. case ETH_P_IP:
  287. return NP_IP;
  288. case ETH_P_IPV6:
  289. return NP_IPV6;
  290. case ETH_P_IPX:
  291. return NP_IPX;
  292. case ETH_P_PPPTALK:
  293. case ETH_P_ATALK:
  294. return NP_AT;
  295. case ETH_P_MPLS_UC:
  296. return NP_MPLS_UC;
  297. case ETH_P_MPLS_MC:
  298. return NP_MPLS_MC;
  299. }
  300. return -1;
  301. }
  302. /* Translates an NP index into an ethertype */
  303. static const int npindex_to_ethertype[NUM_NP] = {
  304. ETH_P_IP,
  305. ETH_P_IPV6,
  306. ETH_P_IPX,
  307. ETH_P_PPPTALK,
  308. ETH_P_MPLS_UC,
  309. ETH_P_MPLS_MC,
  310. };
  311. /*
  312. * Locking shorthand.
  313. */
  314. #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
  315. #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
  316. #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
  317. #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
  318. #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
  319. ppp_recv_lock(ppp); } while (0)
  320. #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
  321. ppp_xmit_unlock(ppp); } while (0)
  322. /*
  323. * /dev/ppp device routines.
  324. * The /dev/ppp device is used by pppd to control the ppp unit.
  325. * It supports the read, write, ioctl and poll functions.
  326. * Open instances of /dev/ppp can be in one of three states:
  327. * unattached, attached to a ppp unit, or attached to a ppp channel.
  328. */
  329. static int ppp_open(struct inode *inode, struct file *file)
  330. {
  331. /*
  332. * This could (should?) be enforced by the permissions on /dev/ppp.
  333. */
  334. if (!capable(CAP_NET_ADMIN))
  335. return -EPERM;
  336. return 0;
  337. }
  338. static int ppp_release(struct inode *inode, struct file *file)
  339. {
  340. struct ppp_file *pf = file->private_data;
  341. struct ppp *ppp;
  342. if (pf != 0) {
  343. file->private_data = NULL;
  344. if (pf->kind == INTERFACE) {
  345. ppp = PF_TO_PPP(pf);
  346. if (file == ppp->owner)
  347. ppp_shutdown_interface(ppp);
  348. }
  349. if (atomic_dec_and_test(&pf->refcnt)) {
  350. switch (pf->kind) {
  351. case INTERFACE:
  352. ppp_destroy_interface(PF_TO_PPP(pf));
  353. break;
  354. case CHANNEL:
  355. ppp_destroy_channel(PF_TO_CHANNEL(pf));
  356. break;
  357. }
  358. }
  359. }
  360. return 0;
  361. }
  362. static ssize_t ppp_read(struct file *file, char __user *buf,
  363. size_t count, loff_t *ppos)
  364. {
  365. struct ppp_file *pf = file->private_data;
  366. DECLARE_WAITQUEUE(wait, current);
  367. ssize_t ret;
  368. struct sk_buff *skb = NULL;
  369. ret = count;
  370. if (pf == 0)
  371. return -ENXIO;
  372. add_wait_queue(&pf->rwait, &wait);
  373. for (;;) {
  374. set_current_state(TASK_INTERRUPTIBLE);
  375. skb = skb_dequeue(&pf->rq);
  376. if (skb)
  377. break;
  378. ret = 0;
  379. if (pf->dead)
  380. break;
  381. if (pf->kind == INTERFACE) {
  382. /*
  383. * Return 0 (EOF) on an interface that has no
  384. * channels connected, unless it is looping
  385. * network traffic (demand mode).
  386. */
  387. struct ppp *ppp = PF_TO_PPP(pf);
  388. if (ppp->n_channels == 0
  389. && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  390. break;
  391. }
  392. ret = -EAGAIN;
  393. if (file->f_flags & O_NONBLOCK)
  394. break;
  395. ret = -ERESTARTSYS;
  396. if (signal_pending(current))
  397. break;
  398. schedule();
  399. }
  400. set_current_state(TASK_RUNNING);
  401. remove_wait_queue(&pf->rwait, &wait);
  402. if (skb == 0)
  403. goto out;
  404. ret = -EOVERFLOW;
  405. if (skb->len > count)
  406. goto outf;
  407. ret = -EFAULT;
  408. if (copy_to_user(buf, skb->data, skb->len))
  409. goto outf;
  410. ret = skb->len;
  411. outf:
  412. kfree_skb(skb);
  413. out:
  414. return ret;
  415. }
  416. static ssize_t ppp_write(struct file *file, const char __user *buf,
  417. size_t count, loff_t *ppos)
  418. {
  419. struct ppp_file *pf = file->private_data;
  420. struct sk_buff *skb;
  421. ssize_t ret;
  422. if (pf == 0)
  423. return -ENXIO;
  424. ret = -ENOMEM;
  425. skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
  426. if (skb == 0)
  427. goto out;
  428. skb_reserve(skb, pf->hdrlen);
  429. ret = -EFAULT;
  430. if (copy_from_user(skb_put(skb, count), buf, count)) {
  431. kfree_skb(skb);
  432. goto out;
  433. }
  434. skb_queue_tail(&pf->xq, skb);
  435. switch (pf->kind) {
  436. case INTERFACE:
  437. ppp_xmit_process(PF_TO_PPP(pf));
  438. break;
  439. case CHANNEL:
  440. ppp_channel_push(PF_TO_CHANNEL(pf));
  441. break;
  442. }
  443. ret = count;
  444. out:
  445. return ret;
  446. }
  447. /* No kernel lock - fine */
  448. static unsigned int ppp_poll(struct file *file, poll_table *wait)
  449. {
  450. struct ppp_file *pf = file->private_data;
  451. unsigned int mask;
  452. if (pf == 0)
  453. return 0;
  454. poll_wait(file, &pf->rwait, wait);
  455. mask = POLLOUT | POLLWRNORM;
  456. if (skb_peek(&pf->rq) != 0)
  457. mask |= POLLIN | POLLRDNORM;
  458. if (pf->dead)
  459. mask |= POLLHUP;
  460. else if (pf->kind == INTERFACE) {
  461. /* see comment in ppp_read */
  462. struct ppp *ppp = PF_TO_PPP(pf);
  463. if (ppp->n_channels == 0
  464. && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  465. mask |= POLLIN | POLLRDNORM;
  466. }
  467. return mask;
  468. }
  469. #ifdef CONFIG_PPP_FILTER
  470. static int get_filter(void __user *arg, struct sock_filter **p)
  471. {
  472. struct sock_fprog uprog;
  473. struct sock_filter *code = NULL;
  474. int len, err;
  475. if (copy_from_user(&uprog, arg, sizeof(uprog)))
  476. return -EFAULT;
  477. if (uprog.len > BPF_MAXINSNS)
  478. return -EINVAL;
  479. if (!uprog.len) {
  480. *p = NULL;
  481. return 0;
  482. }
  483. len = uprog.len * sizeof(struct sock_filter);
  484. code = kmalloc(len, GFP_KERNEL);
  485. if (code == NULL)
  486. return -ENOMEM;
  487. if (copy_from_user(code, uprog.filter, len)) {
  488. kfree(code);
  489. return -EFAULT;
  490. }
  491. err = sk_chk_filter(code, uprog.len);
  492. if (err) {
  493. kfree(code);
  494. return err;
  495. }
  496. *p = code;
  497. return uprog.len;
  498. }
  499. #endif /* CONFIG_PPP_FILTER */
  500. static int ppp_ioctl(struct inode *inode, struct file *file,
  501. unsigned int cmd, unsigned long arg)
  502. {
  503. struct ppp_file *pf = file->private_data;
  504. struct ppp *ppp;
  505. int err = -EFAULT, val, val2, i;
  506. struct ppp_idle idle;
  507. struct npioctl npi;
  508. int unit, cflags;
  509. struct slcompress *vj;
  510. void __user *argp = (void __user *)arg;
  511. int __user *p = argp;
  512. if (pf == 0)
  513. return ppp_unattached_ioctl(pf, file, cmd, arg);
  514. if (cmd == PPPIOCDETACH) {
  515. /*
  516. * We have to be careful here... if the file descriptor
  517. * has been dup'd, we could have another process in the
  518. * middle of a poll using the same file *, so we had
  519. * better not free the interface data structures -
  520. * instead we fail the ioctl. Even in this case, we
  521. * shut down the interface if we are the owner of it.
  522. * Actually, we should get rid of PPPIOCDETACH, userland
  523. * (i.e. pppd) could achieve the same effect by closing
  524. * this fd and reopening /dev/ppp.
  525. */
  526. err = -EINVAL;
  527. if (pf->kind == INTERFACE) {
  528. ppp = PF_TO_PPP(pf);
  529. if (file == ppp->owner)
  530. ppp_shutdown_interface(ppp);
  531. }
  532. if (atomic_read(&file->f_count) <= 2) {
  533. ppp_release(inode, file);
  534. err = 0;
  535. } else
  536. printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
  537. atomic_read(&file->f_count));
  538. return err;
  539. }
  540. if (pf->kind == CHANNEL) {
  541. struct channel *pch = PF_TO_CHANNEL(pf);
  542. struct ppp_channel *chan;
  543. switch (cmd) {
  544. case PPPIOCCONNECT:
  545. if (get_user(unit, p))
  546. break;
  547. err = ppp_connect_channel(pch, unit);
  548. break;
  549. case PPPIOCDISCONN:
  550. err = ppp_disconnect_channel(pch);
  551. break;
  552. default:
  553. down_read(&pch->chan_sem);
  554. chan = pch->chan;
  555. err = -ENOTTY;
  556. if (chan && chan->ops->ioctl)
  557. err = chan->ops->ioctl(chan, cmd, arg);
  558. up_read(&pch->chan_sem);
  559. }
  560. return err;
  561. }
  562. if (pf->kind != INTERFACE) {
  563. /* can't happen */
  564. printk(KERN_ERR "PPP: not interface or channel??\n");
  565. return -EINVAL;
  566. }
  567. ppp = PF_TO_PPP(pf);
  568. switch (cmd) {
  569. case PPPIOCSMRU:
  570. if (get_user(val, p))
  571. break;
  572. ppp->mru = val;
  573. err = 0;
  574. break;
  575. case PPPIOCSFLAGS:
  576. if (get_user(val, p))
  577. break;
  578. ppp_lock(ppp);
  579. cflags = ppp->flags & ~val;
  580. ppp->flags = val & SC_FLAG_BITS;
  581. ppp_unlock(ppp);
  582. if (cflags & SC_CCP_OPEN)
  583. ppp_ccp_closed(ppp);
  584. err = 0;
  585. break;
  586. case PPPIOCGFLAGS:
  587. val = ppp->flags | ppp->xstate | ppp->rstate;
  588. if (put_user(val, p))
  589. break;
  590. err = 0;
  591. break;
  592. case PPPIOCSCOMPRESS:
  593. err = ppp_set_compress(ppp, arg);
  594. break;
  595. case PPPIOCGUNIT:
  596. if (put_user(ppp->file.index, p))
  597. break;
  598. err = 0;
  599. break;
  600. case PPPIOCSDEBUG:
  601. if (get_user(val, p))
  602. break;
  603. ppp->debug = val;
  604. err = 0;
  605. break;
  606. case PPPIOCGDEBUG:
  607. if (put_user(ppp->debug, p))
  608. break;
  609. err = 0;
  610. break;
  611. case PPPIOCGIDLE:
  612. idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  613. idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
  614. if (copy_to_user(argp, &idle, sizeof(idle)))
  615. break;
  616. err = 0;
  617. break;
  618. case PPPIOCSMAXCID:
  619. if (get_user(val, p))
  620. break;
  621. val2 = 15;
  622. if ((val >> 16) != 0) {
  623. val2 = val >> 16;
  624. val &= 0xffff;
  625. }
  626. vj = slhc_init(val2+1, val+1);
  627. if (vj == 0) {
  628. printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
  629. err = -ENOMEM;
  630. break;
  631. }
  632. ppp_lock(ppp);
  633. if (ppp->vj != 0)
  634. slhc_free(ppp->vj);
  635. ppp->vj = vj;
  636. ppp_unlock(ppp);
  637. err = 0;
  638. break;
  639. case PPPIOCGNPMODE:
  640. case PPPIOCSNPMODE:
  641. if (copy_from_user(&npi, argp, sizeof(npi)))
  642. break;
  643. err = proto_to_npindex(npi.protocol);
  644. if (err < 0)
  645. break;
  646. i = err;
  647. if (cmd == PPPIOCGNPMODE) {
  648. err = -EFAULT;
  649. npi.mode = ppp->npmode[i];
  650. if (copy_to_user(argp, &npi, sizeof(npi)))
  651. break;
  652. } else {
  653. ppp->npmode[i] = npi.mode;
  654. /* we may be able to transmit more packets now (??) */
  655. netif_wake_queue(ppp->dev);
  656. }
  657. err = 0;
  658. break;
  659. #ifdef CONFIG_PPP_FILTER
  660. case PPPIOCSPASS:
  661. {
  662. struct sock_filter *code;
  663. err = get_filter(argp, &code);
  664. if (err >= 0) {
  665. ppp_lock(ppp);
  666. kfree(ppp->pass_filter);
  667. ppp->pass_filter = code;
  668. ppp->pass_len = err;
  669. ppp_unlock(ppp);
  670. err = 0;
  671. }
  672. break;
  673. }
  674. case PPPIOCSACTIVE:
  675. {
  676. struct sock_filter *code;
  677. err = get_filter(argp, &code);
  678. if (err >= 0) {
  679. ppp_lock(ppp);
  680. kfree(ppp->active_filter);
  681. ppp->active_filter = code;
  682. ppp->active_len = err;
  683. ppp_unlock(ppp);
  684. err = 0;
  685. }
  686. break;
  687. }
  688. #endif /* CONFIG_PPP_FILTER */
  689. #ifdef CONFIG_PPP_MULTILINK
  690. case PPPIOCSMRRU:
  691. if (get_user(val, p))
  692. break;
  693. ppp_recv_lock(ppp);
  694. ppp->mrru = val;
  695. ppp_recv_unlock(ppp);
  696. err = 0;
  697. break;
  698. #endif /* CONFIG_PPP_MULTILINK */
  699. default:
  700. err = -ENOTTY;
  701. }
  702. return err;
  703. }
  704. static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
  705. unsigned int cmd, unsigned long arg)
  706. {
  707. int unit, err = -EFAULT;
  708. struct ppp *ppp;
  709. struct channel *chan;
  710. int __user *p = (int __user *)arg;
  711. switch (cmd) {
  712. case PPPIOCNEWUNIT:
  713. /* Create a new ppp unit */
  714. if (get_user(unit, p))
  715. break;
  716. ppp = ppp_create_interface(unit, &err);
  717. if (ppp == 0)
  718. break;
  719. file->private_data = &ppp->file;
  720. ppp->owner = file;
  721. err = -EFAULT;
  722. if (put_user(ppp->file.index, p))
  723. break;
  724. err = 0;
  725. break;
  726. case PPPIOCATTACH:
  727. /* Attach to an existing ppp unit */
  728. if (get_user(unit, p))
  729. break;
  730. down(&all_ppp_sem);
  731. err = -ENXIO;
  732. ppp = ppp_find_unit(unit);
  733. if (ppp != 0) {
  734. atomic_inc(&ppp->file.refcnt);
  735. file->private_data = &ppp->file;
  736. err = 0;
  737. }
  738. up(&all_ppp_sem);
  739. break;
  740. case PPPIOCATTCHAN:
  741. if (get_user(unit, p))
  742. break;
  743. spin_lock_bh(&all_channels_lock);
  744. err = -ENXIO;
  745. chan = ppp_find_channel(unit);
  746. if (chan != 0) {
  747. atomic_inc(&chan->file.refcnt);
  748. file->private_data = &chan->file;
  749. err = 0;
  750. }
  751. spin_unlock_bh(&all_channels_lock);
  752. break;
  753. default:
  754. err = -ENOTTY;
  755. }
  756. return err;
  757. }
  758. static struct file_operations ppp_device_fops = {
  759. .owner = THIS_MODULE,
  760. .read = ppp_read,
  761. .write = ppp_write,
  762. .poll = ppp_poll,
  763. .ioctl = ppp_ioctl,
  764. .open = ppp_open,
  765. .release = ppp_release
  766. };
  767. #define PPP_MAJOR 108
  768. /* Called at boot time if ppp is compiled into the kernel,
  769. or at module load time (from init_module) if compiled as a module. */
  770. static int __init ppp_init(void)
  771. {
  772. int err;
  773. printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
  774. err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
  775. if (!err) {
  776. ppp_class = class_simple_create(THIS_MODULE, "ppp");
  777. if (IS_ERR(ppp_class)) {
  778. err = PTR_ERR(ppp_class);
  779. goto out_chrdev;
  780. }
  781. class_simple_device_add(ppp_class, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
  782. err = devfs_mk_cdev(MKDEV(PPP_MAJOR, 0),
  783. S_IFCHR|S_IRUSR|S_IWUSR, "ppp");
  784. if (err)
  785. goto out_class;
  786. }
  787. out:
  788. if (err)
  789. printk(KERN_ERR "failed to register PPP device (%d)\n", err);
  790. return err;
  791. out_class:
  792. class_simple_device_remove(MKDEV(PPP_MAJOR,0));
  793. class_simple_destroy(ppp_class);
  794. out_chrdev:
  795. unregister_chrdev(PPP_MAJOR, "ppp");
  796. goto out;
  797. }
  798. /*
  799. * Network interface unit routines.
  800. */
  801. static int
  802. ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  803. {
  804. struct ppp *ppp = (struct ppp *) dev->priv;
  805. int npi, proto;
  806. unsigned char *pp;
  807. npi = ethertype_to_npindex(ntohs(skb->protocol));
  808. if (npi < 0)
  809. goto outf;
  810. /* Drop, accept or reject the packet */
  811. switch (ppp->npmode[npi]) {
  812. case NPMODE_PASS:
  813. break;
  814. case NPMODE_QUEUE:
  815. /* it would be nice to have a way to tell the network
  816. system to queue this one up for later. */
  817. goto outf;
  818. case NPMODE_DROP:
  819. case NPMODE_ERROR:
  820. goto outf;
  821. }
  822. /* Put the 2-byte PPP protocol number on the front,
  823. making sure there is room for the address and control fields. */
  824. if (skb_headroom(skb) < PPP_HDRLEN) {
  825. struct sk_buff *ns;
  826. ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
  827. if (ns == 0)
  828. goto outf;
  829. skb_reserve(ns, dev->hard_header_len);
  830. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  831. kfree_skb(skb);
  832. skb = ns;
  833. }
  834. pp = skb_push(skb, 2);
  835. proto = npindex_to_proto[npi];
  836. pp[0] = proto >> 8;
  837. pp[1] = proto;
  838. netif_stop_queue(dev);
  839. skb_queue_tail(&ppp->file.xq, skb);
  840. ppp_xmit_process(ppp);
  841. return 0;
  842. outf:
  843. kfree_skb(skb);
  844. ++ppp->stats.tx_dropped;
  845. return 0;
  846. }
  847. static struct net_device_stats *
  848. ppp_net_stats(struct net_device *dev)
  849. {
  850. struct ppp *ppp = (struct ppp *) dev->priv;
  851. return &ppp->stats;
  852. }
  853. static int
  854. ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  855. {
  856. struct ppp *ppp = dev->priv;
  857. int err = -EFAULT;
  858. void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
  859. struct ppp_stats stats;
  860. struct ppp_comp_stats cstats;
  861. char *vers;
  862. switch (cmd) {
  863. case SIOCGPPPSTATS:
  864. ppp_get_stats(ppp, &stats);
  865. if (copy_to_user(addr, &stats, sizeof(stats)))
  866. break;
  867. err = 0;
  868. break;
  869. case SIOCGPPPCSTATS:
  870. memset(&cstats, 0, sizeof(cstats));
  871. if (ppp->xc_state != 0)
  872. ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
  873. if (ppp->rc_state != 0)
  874. ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
  875. if (copy_to_user(addr, &cstats, sizeof(cstats)))
  876. break;
  877. err = 0;
  878. break;
  879. case SIOCGPPPVER:
  880. vers = PPP_VERSION;
  881. if (copy_to_user(addr, vers, strlen(vers) + 1))
  882. break;
  883. err = 0;
  884. break;
  885. default:
  886. err = -EINVAL;
  887. }
  888. return err;
  889. }
  890. static void ppp_setup(struct net_device *dev)
  891. {
  892. dev->hard_header_len = PPP_HDRLEN;
  893. dev->mtu = PPP_MTU;
  894. dev->addr_len = 0;
  895. dev->tx_queue_len = 3;
  896. dev->type = ARPHRD_PPP;
  897. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  898. }
  899. /*
  900. * Transmit-side routines.
  901. */
  902. /*
  903. * Called to do any work queued up on the transmit side
  904. * that can now be done.
  905. */
  906. static void
  907. ppp_xmit_process(struct ppp *ppp)
  908. {
  909. struct sk_buff *skb;
  910. ppp_xmit_lock(ppp);
  911. if (ppp->dev != 0) {
  912. ppp_push(ppp);
  913. while (ppp->xmit_pending == 0
  914. && (skb = skb_dequeue(&ppp->file.xq)) != 0)
  915. ppp_send_frame(ppp, skb);
  916. /* If there's no work left to do, tell the core net
  917. code that we can accept some more. */
  918. if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
  919. netif_wake_queue(ppp->dev);
  920. }
  921. ppp_xmit_unlock(ppp);
  922. }
  923. /*
  924. * Compress and send a frame.
  925. * The caller should have locked the xmit path,
  926. * and xmit_pending should be 0.
  927. */
  928. static void
  929. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  930. {
  931. int proto = PPP_PROTO(skb);
  932. struct sk_buff *new_skb;
  933. int len;
  934. unsigned char *cp;
  935. if (proto < 0x8000) {
  936. #ifdef CONFIG_PPP_FILTER
  937. /* check if we should pass this packet */
  938. /* the filter instructions are constructed assuming
  939. a four-byte PPP header on each packet */
  940. *skb_push(skb, 2) = 1;
  941. if (ppp->pass_filter
  942. && sk_run_filter(skb, ppp->pass_filter,
  943. ppp->pass_len) == 0) {
  944. if (ppp->debug & 1)
  945. printk(KERN_DEBUG "PPP: outbound frame not passed\n");
  946. kfree_skb(skb);
  947. return;
  948. }
  949. /* if this packet passes the active filter, record the time */
  950. if (!(ppp->active_filter
  951. && sk_run_filter(skb, ppp->active_filter,
  952. ppp->active_len) == 0))
  953. ppp->last_xmit = jiffies;
  954. skb_pull(skb, 2);
  955. #else
  956. /* for data packets, record the time */
  957. ppp->last_xmit = jiffies;
  958. #endif /* CONFIG_PPP_FILTER */
  959. }
  960. ++ppp->stats.tx_packets;
  961. ppp->stats.tx_bytes += skb->len - 2;
  962. switch (proto) {
  963. case PPP_IP:
  964. if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
  965. break;
  966. /* try to do VJ TCP header compression */
  967. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  968. GFP_ATOMIC);
  969. if (new_skb == 0) {
  970. printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
  971. goto drop;
  972. }
  973. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  974. cp = skb->data + 2;
  975. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  976. new_skb->data + 2, &cp,
  977. !(ppp->flags & SC_NO_TCP_CCID));
  978. if (cp == skb->data + 2) {
  979. /* didn't compress */
  980. kfree_skb(new_skb);
  981. } else {
  982. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  983. proto = PPP_VJC_COMP;
  984. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  985. } else {
  986. proto = PPP_VJC_UNCOMP;
  987. cp[0] = skb->data[2];
  988. }
  989. kfree_skb(skb);
  990. skb = new_skb;
  991. cp = skb_put(skb, len + 2);
  992. cp[0] = 0;
  993. cp[1] = proto;
  994. }
  995. break;
  996. case PPP_CCP:
  997. /* peek at outbound CCP frames */
  998. ppp_ccp_peek(ppp, skb, 0);
  999. break;
  1000. }
  1001. /* try to do packet compression */
  1002. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
  1003. && proto != PPP_LCP && proto != PPP_CCP) {
  1004. new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
  1005. GFP_ATOMIC);
  1006. if (new_skb == 0) {
  1007. printk(KERN_ERR "PPP: no memory (comp pkt)\n");
  1008. goto drop;
  1009. }
  1010. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  1011. skb_reserve(new_skb,
  1012. ppp->dev->hard_header_len - PPP_HDRLEN);
  1013. /* compressor still expects A/C bytes in hdr */
  1014. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  1015. new_skb->data, skb->len + 2,
  1016. ppp->dev->mtu + PPP_HDRLEN);
  1017. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  1018. kfree_skb(skb);
  1019. skb = new_skb;
  1020. skb_put(skb, len);
  1021. skb_pull(skb, 2); /* pull off A/C bytes */
  1022. } else {
  1023. /* didn't compress, or CCP not up yet */
  1024. kfree_skb(new_skb);
  1025. }
  1026. }
  1027. /*
  1028. * If we are waiting for traffic (demand dialling),
  1029. * queue it up for pppd to receive.
  1030. */
  1031. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1032. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1033. goto drop;
  1034. skb_queue_tail(&ppp->file.rq, skb);
  1035. wake_up_interruptible(&ppp->file.rwait);
  1036. return;
  1037. }
  1038. ppp->xmit_pending = skb;
  1039. ppp_push(ppp);
  1040. return;
  1041. drop:
  1042. kfree_skb(skb);
  1043. ++ppp->stats.tx_errors;
  1044. }
  1045. /*
  1046. * Try to send the frame in xmit_pending.
  1047. * The caller should have the xmit path locked.
  1048. */
  1049. static void
  1050. ppp_push(struct ppp *ppp)
  1051. {
  1052. struct list_head *list;
  1053. struct channel *pch;
  1054. struct sk_buff *skb = ppp->xmit_pending;
  1055. if (skb == 0)
  1056. return;
  1057. list = &ppp->channels;
  1058. if (list_empty(list)) {
  1059. /* nowhere to send the packet, just drop it */
  1060. ppp->xmit_pending = NULL;
  1061. kfree_skb(skb);
  1062. return;
  1063. }
  1064. if ((ppp->flags & SC_MULTILINK) == 0) {
  1065. /* not doing multilink: send it down the first channel */
  1066. list = list->next;
  1067. pch = list_entry(list, struct channel, clist);
  1068. spin_lock_bh(&pch->downl);
  1069. if (pch->chan) {
  1070. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1071. ppp->xmit_pending = NULL;
  1072. } else {
  1073. /* channel got unregistered */
  1074. kfree_skb(skb);
  1075. ppp->xmit_pending = NULL;
  1076. }
  1077. spin_unlock_bh(&pch->downl);
  1078. return;
  1079. }
  1080. #ifdef CONFIG_PPP_MULTILINK
  1081. /* Multilink: fragment the packet over as many links
  1082. as can take the packet at the moment. */
  1083. if (!ppp_mp_explode(ppp, skb))
  1084. return;
  1085. #endif /* CONFIG_PPP_MULTILINK */
  1086. ppp->xmit_pending = NULL;
  1087. kfree_skb(skb);
  1088. }
  1089. #ifdef CONFIG_PPP_MULTILINK
  1090. /*
  1091. * Divide a packet to be transmitted into fragments and
  1092. * send them out the individual links.
  1093. */
  1094. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1095. {
  1096. int nch, len, fragsize;
  1097. int i, bits, hdrlen, mtu;
  1098. int flen, fnb;
  1099. unsigned char *p, *q;
  1100. struct list_head *list;
  1101. struct channel *pch;
  1102. struct sk_buff *frag;
  1103. struct ppp_channel *chan;
  1104. nch = 0;
  1105. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1106. list = &ppp->channels;
  1107. while ((list = list->next) != &ppp->channels) {
  1108. pch = list_entry(list, struct channel, clist);
  1109. nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
  1110. /*
  1111. * If a channel hasn't had a fragment yet, it has to get
  1112. * one before we send any fragments on later channels.
  1113. * If it can't take a fragment now, don't give any
  1114. * to subsequent channels.
  1115. */
  1116. if (!pch->had_frag && !pch->avail) {
  1117. while ((list = list->next) != &ppp->channels) {
  1118. pch = list_entry(list, struct channel, clist);
  1119. pch->avail = 0;
  1120. }
  1121. break;
  1122. }
  1123. }
  1124. if (nch == 0)
  1125. return 0; /* can't take now, leave it in xmit_pending */
  1126. /* Do protocol field compression (XXX this should be optional) */
  1127. p = skb->data;
  1128. len = skb->len;
  1129. if (*p == 0) {
  1130. ++p;
  1131. --len;
  1132. }
  1133. /* decide on fragment size */
  1134. fragsize = len;
  1135. if (nch > 1) {
  1136. int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
  1137. if (nch > maxch)
  1138. nch = maxch;
  1139. fragsize = ROUNDUP(fragsize, nch);
  1140. }
  1141. /* skip to the channel after the one we last used
  1142. and start at that one */
  1143. for (i = 0; i < ppp->nxchan; ++i) {
  1144. list = list->next;
  1145. if (list == &ppp->channels) {
  1146. i = 0;
  1147. break;
  1148. }
  1149. }
  1150. /* create a fragment for each channel */
  1151. bits = B;
  1152. do {
  1153. list = list->next;
  1154. if (list == &ppp->channels) {
  1155. i = 0;
  1156. continue;
  1157. }
  1158. pch = list_entry(list, struct channel, clist);
  1159. ++i;
  1160. if (!pch->avail)
  1161. continue;
  1162. /* check the channel's mtu and whether it is still attached. */
  1163. spin_lock_bh(&pch->downl);
  1164. if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
  1165. /* can't use this channel */
  1166. spin_unlock_bh(&pch->downl);
  1167. pch->avail = 0;
  1168. if (--nch == 0)
  1169. break;
  1170. continue;
  1171. }
  1172. /*
  1173. * We have to create multiple fragments for this channel
  1174. * if fragsize is greater than the channel's mtu.
  1175. */
  1176. if (fragsize > len)
  1177. fragsize = len;
  1178. for (flen = fragsize; flen > 0; flen -= fnb) {
  1179. fnb = flen;
  1180. if (fnb > mtu + 2 - hdrlen)
  1181. fnb = mtu + 2 - hdrlen;
  1182. if (fnb >= len)
  1183. bits |= E;
  1184. frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
  1185. if (frag == 0)
  1186. goto noskb;
  1187. q = skb_put(frag, fnb + hdrlen);
  1188. /* make the MP header */
  1189. q[0] = PPP_MP >> 8;
  1190. q[1] = PPP_MP;
  1191. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1192. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1193. q[3] = ppp->nxseq;
  1194. } else {
  1195. q[2] = bits;
  1196. q[3] = ppp->nxseq >> 16;
  1197. q[4] = ppp->nxseq >> 8;
  1198. q[5] = ppp->nxseq;
  1199. }
  1200. /* copy the data in */
  1201. memcpy(q + hdrlen, p, fnb);
  1202. /* try to send it down the channel */
  1203. chan = pch->chan;
  1204. if (!chan->ops->start_xmit(chan, frag))
  1205. skb_queue_tail(&pch->file.xq, frag);
  1206. pch->had_frag = 1;
  1207. p += fnb;
  1208. len -= fnb;
  1209. ++ppp->nxseq;
  1210. bits = 0;
  1211. }
  1212. spin_unlock_bh(&pch->downl);
  1213. } while (len > 0);
  1214. ppp->nxchan = i;
  1215. return 1;
  1216. noskb:
  1217. spin_unlock_bh(&pch->downl);
  1218. if (ppp->debug & 1)
  1219. printk(KERN_ERR "PPP: no memory (fragment)\n");
  1220. ++ppp->stats.tx_errors;
  1221. ++ppp->nxseq;
  1222. return 1; /* abandon the frame */
  1223. }
  1224. #endif /* CONFIG_PPP_MULTILINK */
  1225. /*
  1226. * Try to send data out on a channel.
  1227. */
  1228. static void
  1229. ppp_channel_push(struct channel *pch)
  1230. {
  1231. struct sk_buff *skb;
  1232. struct ppp *ppp;
  1233. spin_lock_bh(&pch->downl);
  1234. if (pch->chan != 0) {
  1235. while (skb_queue_len(&pch->file.xq) > 0) {
  1236. skb = skb_dequeue(&pch->file.xq);
  1237. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1238. /* put the packet back and try again later */
  1239. skb_queue_head(&pch->file.xq, skb);
  1240. break;
  1241. }
  1242. }
  1243. } else {
  1244. /* channel got deregistered */
  1245. skb_queue_purge(&pch->file.xq);
  1246. }
  1247. spin_unlock_bh(&pch->downl);
  1248. /* see if there is anything from the attached unit to be sent */
  1249. if (skb_queue_len(&pch->file.xq) == 0) {
  1250. read_lock_bh(&pch->upl);
  1251. ppp = pch->ppp;
  1252. if (ppp != 0)
  1253. ppp_xmit_process(ppp);
  1254. read_unlock_bh(&pch->upl);
  1255. }
  1256. }
  1257. /*
  1258. * Receive-side routines.
  1259. */
  1260. /* misuse a few fields of the skb for MP reconstruction */
  1261. #define sequence priority
  1262. #define BEbits cb[0]
  1263. static inline void
  1264. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1265. {
  1266. ppp_recv_lock(ppp);
  1267. /* ppp->dev == 0 means interface is closing down */
  1268. if (ppp->dev != 0)
  1269. ppp_receive_frame(ppp, skb, pch);
  1270. else
  1271. kfree_skb(skb);
  1272. ppp_recv_unlock(ppp);
  1273. }
  1274. void
  1275. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  1276. {
  1277. struct channel *pch = chan->ppp;
  1278. int proto;
  1279. if (pch == 0 || skb->len == 0) {
  1280. kfree_skb(skb);
  1281. return;
  1282. }
  1283. proto = PPP_PROTO(skb);
  1284. read_lock_bh(&pch->upl);
  1285. if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  1286. /* put it on the channel queue */
  1287. skb_queue_tail(&pch->file.rq, skb);
  1288. /* drop old frames if queue too long */
  1289. while (pch->file.rq.qlen > PPP_MAX_RQLEN
  1290. && (skb = skb_dequeue(&pch->file.rq)) != 0)
  1291. kfree_skb(skb);
  1292. wake_up_interruptible(&pch->file.rwait);
  1293. } else {
  1294. ppp_do_recv(pch->ppp, skb, pch);
  1295. }
  1296. read_unlock_bh(&pch->upl);
  1297. }
  1298. /* Put a 0-length skb in the receive queue as an error indication */
  1299. void
  1300. ppp_input_error(struct ppp_channel *chan, int code)
  1301. {
  1302. struct channel *pch = chan->ppp;
  1303. struct sk_buff *skb;
  1304. if (pch == 0)
  1305. return;
  1306. read_lock_bh(&pch->upl);
  1307. if (pch->ppp != 0) {
  1308. skb = alloc_skb(0, GFP_ATOMIC);
  1309. if (skb != 0) {
  1310. skb->len = 0; /* probably unnecessary */
  1311. skb->cb[0] = code;
  1312. ppp_do_recv(pch->ppp, skb, pch);
  1313. }
  1314. }
  1315. read_unlock_bh(&pch->upl);
  1316. }
  1317. /*
  1318. * We come in here to process a received frame.
  1319. * The receive side of the ppp unit is locked.
  1320. */
  1321. static void
  1322. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1323. {
  1324. if (skb->len >= 2) {
  1325. #ifdef CONFIG_PPP_MULTILINK
  1326. /* XXX do channel-level decompression here */
  1327. if (PPP_PROTO(skb) == PPP_MP)
  1328. ppp_receive_mp_frame(ppp, skb, pch);
  1329. else
  1330. #endif /* CONFIG_PPP_MULTILINK */
  1331. ppp_receive_nonmp_frame(ppp, skb);
  1332. return;
  1333. }
  1334. if (skb->len > 0)
  1335. /* note: a 0-length skb is used as an error indication */
  1336. ++ppp->stats.rx_length_errors;
  1337. kfree_skb(skb);
  1338. ppp_receive_error(ppp);
  1339. }
  1340. static void
  1341. ppp_receive_error(struct ppp *ppp)
  1342. {
  1343. ++ppp->stats.rx_errors;
  1344. if (ppp->vj != 0)
  1345. slhc_toss(ppp->vj);
  1346. }
  1347. static void
  1348. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  1349. {
  1350. struct sk_buff *ns;
  1351. int proto, len, npi;
  1352. /*
  1353. * Decompress the frame, if compressed.
  1354. * Note that some decompressors need to see uncompressed frames
  1355. * that come in as well as compressed frames.
  1356. */
  1357. if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
  1358. && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  1359. skb = ppp_decompress_frame(ppp, skb);
  1360. proto = PPP_PROTO(skb);
  1361. switch (proto) {
  1362. case PPP_VJC_COMP:
  1363. /* decompress VJ compressed packets */
  1364. if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
  1365. goto err;
  1366. if (skb_tailroom(skb) < 124) {
  1367. /* copy to a new sk_buff with more tailroom */
  1368. ns = dev_alloc_skb(skb->len + 128);
  1369. if (ns == 0) {
  1370. printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
  1371. goto err;
  1372. }
  1373. skb_reserve(ns, 2);
  1374. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  1375. kfree_skb(skb);
  1376. skb = ns;
  1377. }
  1378. else if (!pskb_may_pull(skb, skb->len))
  1379. goto err;
  1380. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  1381. if (len <= 0) {
  1382. printk(KERN_DEBUG "PPP: VJ decompression error\n");
  1383. goto err;
  1384. }
  1385. len += 2;
  1386. if (len > skb->len)
  1387. skb_put(skb, len - skb->len);
  1388. else if (len < skb->len)
  1389. skb_trim(skb, len);
  1390. proto = PPP_IP;
  1391. break;
  1392. case PPP_VJC_UNCOMP:
  1393. if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
  1394. goto err;
  1395. /* Until we fix the decompressor need to make sure
  1396. * data portion is linear.
  1397. */
  1398. if (!pskb_may_pull(skb, skb->len))
  1399. goto err;
  1400. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  1401. printk(KERN_ERR "PPP: VJ uncompressed error\n");
  1402. goto err;
  1403. }
  1404. proto = PPP_IP;
  1405. break;
  1406. case PPP_CCP:
  1407. ppp_ccp_peek(ppp, skb, 1);
  1408. break;
  1409. }
  1410. ++ppp->stats.rx_packets;
  1411. ppp->stats.rx_bytes += skb->len - 2;
  1412. npi = proto_to_npindex(proto);
  1413. if (npi < 0) {
  1414. /* control or unknown frame - pass it to pppd */
  1415. skb_queue_tail(&ppp->file.rq, skb);
  1416. /* limit queue length by dropping old frames */
  1417. while (ppp->file.rq.qlen > PPP_MAX_RQLEN
  1418. && (skb = skb_dequeue(&ppp->file.rq)) != 0)
  1419. kfree_skb(skb);
  1420. /* wake up any process polling or blocking on read */
  1421. wake_up_interruptible(&ppp->file.rwait);
  1422. } else {
  1423. /* network protocol frame - give it to the kernel */
  1424. #ifdef CONFIG_PPP_FILTER
  1425. /* check if the packet passes the pass and active filters */
  1426. /* the filter instructions are constructed assuming
  1427. a four-byte PPP header on each packet */
  1428. *skb_push(skb, 2) = 0;
  1429. if (ppp->pass_filter
  1430. && sk_run_filter(skb, ppp->pass_filter,
  1431. ppp->pass_len) == 0) {
  1432. if (ppp->debug & 1)
  1433. printk(KERN_DEBUG "PPP: inbound frame not passed\n");
  1434. kfree_skb(skb);
  1435. return;
  1436. }
  1437. if (!(ppp->active_filter
  1438. && sk_run_filter(skb, ppp->active_filter,
  1439. ppp->active_len) == 0))
  1440. ppp->last_recv = jiffies;
  1441. skb_pull(skb, 2);
  1442. #else
  1443. ppp->last_recv = jiffies;
  1444. #endif /* CONFIG_PPP_FILTER */
  1445. if ((ppp->dev->flags & IFF_UP) == 0
  1446. || ppp->npmode[npi] != NPMODE_PASS) {
  1447. kfree_skb(skb);
  1448. } else {
  1449. skb_pull(skb, 2); /* chop off protocol */
  1450. skb->dev = ppp->dev;
  1451. skb->protocol = htons(npindex_to_ethertype[npi]);
  1452. skb->mac.raw = skb->data;
  1453. skb->input_dev = ppp->dev;
  1454. netif_rx(skb);
  1455. ppp->dev->last_rx = jiffies;
  1456. }
  1457. }
  1458. return;
  1459. err:
  1460. kfree_skb(skb);
  1461. ppp_receive_error(ppp);
  1462. }
  1463. static struct sk_buff *
  1464. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  1465. {
  1466. int proto = PPP_PROTO(skb);
  1467. struct sk_buff *ns;
  1468. int len;
  1469. /* Until we fix all the decompressor's need to make sure
  1470. * data portion is linear.
  1471. */
  1472. if (!pskb_may_pull(skb, skb->len))
  1473. goto err;
  1474. if (proto == PPP_COMP) {
  1475. ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
  1476. if (ns == 0) {
  1477. printk(KERN_ERR "ppp_decompress_frame: no memory\n");
  1478. goto err;
  1479. }
  1480. /* the decompressor still expects the A/C bytes in the hdr */
  1481. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  1482. skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
  1483. if (len < 0) {
  1484. /* Pass the compressed frame to pppd as an
  1485. error indication. */
  1486. if (len == DECOMP_FATALERROR)
  1487. ppp->rstate |= SC_DC_FERROR;
  1488. kfree_skb(ns);
  1489. goto err;
  1490. }
  1491. kfree_skb(skb);
  1492. skb = ns;
  1493. skb_put(skb, len);
  1494. skb_pull(skb, 2); /* pull off the A/C bytes */
  1495. } else {
  1496. /* Uncompressed frame - pass to decompressor so it
  1497. can update its dictionary if necessary. */
  1498. if (ppp->rcomp->incomp)
  1499. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  1500. skb->len + 2);
  1501. }
  1502. return skb;
  1503. err:
  1504. ppp->rstate |= SC_DC_ERROR;
  1505. ppp_receive_error(ppp);
  1506. return skb;
  1507. }
  1508. #ifdef CONFIG_PPP_MULTILINK
  1509. /*
  1510. * Receive a multilink frame.
  1511. * We put it on the reconstruction queue and then pull off
  1512. * as many completed frames as we can.
  1513. */
  1514. static void
  1515. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1516. {
  1517. u32 mask, seq;
  1518. struct list_head *l;
  1519. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1520. if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
  1521. goto err; /* no good, throw it away */
  1522. /* Decode sequence number and begin/end bits */
  1523. if (ppp->flags & SC_MP_SHORTSEQ) {
  1524. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  1525. mask = 0xfff;
  1526. } else {
  1527. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  1528. mask = 0xffffff;
  1529. }
  1530. skb->BEbits = skb->data[2];
  1531. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  1532. /*
  1533. * Do protocol ID decompression on the first fragment of each packet.
  1534. */
  1535. if ((skb->BEbits & B) && (skb->data[0] & 1))
  1536. *skb_push(skb, 1) = 0;
  1537. /*
  1538. * Expand sequence number to 32 bits, making it as close
  1539. * as possible to ppp->minseq.
  1540. */
  1541. seq |= ppp->minseq & ~mask;
  1542. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  1543. seq += mask + 1;
  1544. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  1545. seq -= mask + 1; /* should never happen */
  1546. skb->sequence = seq;
  1547. pch->lastseq = seq;
  1548. /*
  1549. * If this packet comes before the next one we were expecting,
  1550. * drop it.
  1551. */
  1552. if (seq_before(seq, ppp->nextseq)) {
  1553. kfree_skb(skb);
  1554. ++ppp->stats.rx_dropped;
  1555. ppp_receive_error(ppp);
  1556. return;
  1557. }
  1558. /*
  1559. * Reevaluate minseq, the minimum over all channels of the
  1560. * last sequence number received on each channel. Because of
  1561. * the increasing sequence number rule, we know that any fragment
  1562. * before `minseq' which hasn't arrived is never going to arrive.
  1563. * The list of channels can't change because we have the receive
  1564. * side of the ppp unit locked.
  1565. */
  1566. for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
  1567. struct channel *ch = list_entry(l, struct channel, clist);
  1568. if (seq_before(ch->lastseq, seq))
  1569. seq = ch->lastseq;
  1570. }
  1571. if (seq_before(ppp->minseq, seq))
  1572. ppp->minseq = seq;
  1573. /* Put the fragment on the reconstruction queue */
  1574. ppp_mp_insert(ppp, skb);
  1575. /* If the queue is getting long, don't wait any longer for packets
  1576. before the start of the queue. */
  1577. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
  1578. && seq_before(ppp->minseq, ppp->mrq.next->sequence))
  1579. ppp->minseq = ppp->mrq.next->sequence;
  1580. /* Pull completed packets off the queue and receive them. */
  1581. while ((skb = ppp_mp_reconstruct(ppp)) != 0)
  1582. ppp_receive_nonmp_frame(ppp, skb);
  1583. return;
  1584. err:
  1585. kfree_skb(skb);
  1586. ppp_receive_error(ppp);
  1587. }
  1588. /*
  1589. * Insert a fragment on the MP reconstruction queue.
  1590. * The queue is ordered by increasing sequence number.
  1591. */
  1592. static void
  1593. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  1594. {
  1595. struct sk_buff *p;
  1596. struct sk_buff_head *list = &ppp->mrq;
  1597. u32 seq = skb->sequence;
  1598. /* N.B. we don't need to lock the list lock because we have the
  1599. ppp unit receive-side lock. */
  1600. for (p = list->next; p != (struct sk_buff *)list; p = p->next)
  1601. if (seq_before(seq, p->sequence))
  1602. break;
  1603. __skb_insert(skb, p->prev, p, list);
  1604. }
  1605. /*
  1606. * Reconstruct a packet from the MP fragment queue.
  1607. * We go through increasing sequence numbers until we find a
  1608. * complete packet, or we get to the sequence number for a fragment
  1609. * which hasn't arrived but might still do so.
  1610. */
  1611. struct sk_buff *
  1612. ppp_mp_reconstruct(struct ppp *ppp)
  1613. {
  1614. u32 seq = ppp->nextseq;
  1615. u32 minseq = ppp->minseq;
  1616. struct sk_buff_head *list = &ppp->mrq;
  1617. struct sk_buff *p, *next;
  1618. struct sk_buff *head, *tail;
  1619. struct sk_buff *skb = NULL;
  1620. int lost = 0, len = 0;
  1621. if (ppp->mrru == 0) /* do nothing until mrru is set */
  1622. return NULL;
  1623. head = list->next;
  1624. tail = NULL;
  1625. for (p = head; p != (struct sk_buff *) list; p = next) {
  1626. next = p->next;
  1627. if (seq_before(p->sequence, seq)) {
  1628. /* this can't happen, anyway ignore the skb */
  1629. printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
  1630. p->sequence, seq);
  1631. head = next;
  1632. continue;
  1633. }
  1634. if (p->sequence != seq) {
  1635. /* Fragment `seq' is missing. If it is after
  1636. minseq, it might arrive later, so stop here. */
  1637. if (seq_after(seq, minseq))
  1638. break;
  1639. /* Fragment `seq' is lost, keep going. */
  1640. lost = 1;
  1641. seq = seq_before(minseq, p->sequence)?
  1642. minseq + 1: p->sequence;
  1643. next = p;
  1644. continue;
  1645. }
  1646. /*
  1647. * At this point we know that all the fragments from
  1648. * ppp->nextseq to seq are either present or lost.
  1649. * Also, there are no complete packets in the queue
  1650. * that have no missing fragments and end before this
  1651. * fragment.
  1652. */
  1653. /* B bit set indicates this fragment starts a packet */
  1654. if (p->BEbits & B) {
  1655. head = p;
  1656. lost = 0;
  1657. len = 0;
  1658. }
  1659. len += p->len;
  1660. /* Got a complete packet yet? */
  1661. if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
  1662. if (len > ppp->mrru + 2) {
  1663. ++ppp->stats.rx_length_errors;
  1664. printk(KERN_DEBUG "PPP: reconstructed packet"
  1665. " is too long (%d)\n", len);
  1666. } else if (p == head) {
  1667. /* fragment is complete packet - reuse skb */
  1668. tail = p;
  1669. skb = skb_get(p);
  1670. break;
  1671. } else if ((skb = dev_alloc_skb(len)) == NULL) {
  1672. ++ppp->stats.rx_missed_errors;
  1673. printk(KERN_DEBUG "PPP: no memory for "
  1674. "reconstructed packet");
  1675. } else {
  1676. tail = p;
  1677. break;
  1678. }
  1679. ppp->nextseq = seq + 1;
  1680. }
  1681. /*
  1682. * If this is the ending fragment of a packet,
  1683. * and we haven't found a complete valid packet yet,
  1684. * we can discard up to and including this fragment.
  1685. */
  1686. if (p->BEbits & E)
  1687. head = next;
  1688. ++seq;
  1689. }
  1690. /* If we have a complete packet, copy it all into one skb. */
  1691. if (tail != NULL) {
  1692. /* If we have discarded any fragments,
  1693. signal a receive error. */
  1694. if (head->sequence != ppp->nextseq) {
  1695. if (ppp->debug & 1)
  1696. printk(KERN_DEBUG " missed pkts %u..%u\n",
  1697. ppp->nextseq, head->sequence-1);
  1698. ++ppp->stats.rx_dropped;
  1699. ppp_receive_error(ppp);
  1700. }
  1701. if (head != tail)
  1702. /* copy to a single skb */
  1703. for (p = head; p != tail->next; p = p->next)
  1704. skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
  1705. ppp->nextseq = tail->sequence + 1;
  1706. head = tail->next;
  1707. }
  1708. /* Discard all the skbuffs that we have copied the data out of
  1709. or that we can't use. */
  1710. while ((p = list->next) != head) {
  1711. __skb_unlink(p, list);
  1712. kfree_skb(p);
  1713. }
  1714. return skb;
  1715. }
  1716. #endif /* CONFIG_PPP_MULTILINK */
  1717. /*
  1718. * Channel interface.
  1719. */
  1720. /*
  1721. * Create a new, unattached ppp channel.
  1722. */
  1723. int
  1724. ppp_register_channel(struct ppp_channel *chan)
  1725. {
  1726. struct channel *pch;
  1727. pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
  1728. if (pch == 0)
  1729. return -ENOMEM;
  1730. memset(pch, 0, sizeof(struct channel));
  1731. pch->ppp = NULL;
  1732. pch->chan = chan;
  1733. chan->ppp = pch;
  1734. init_ppp_file(&pch->file, CHANNEL);
  1735. pch->file.hdrlen = chan->hdrlen;
  1736. #ifdef CONFIG_PPP_MULTILINK
  1737. pch->lastseq = -1;
  1738. #endif /* CONFIG_PPP_MULTILINK */
  1739. init_rwsem(&pch->chan_sem);
  1740. spin_lock_init(&pch->downl);
  1741. rwlock_init(&pch->upl);
  1742. spin_lock_bh(&all_channels_lock);
  1743. pch->file.index = ++last_channel_index;
  1744. list_add(&pch->list, &new_channels);
  1745. atomic_inc(&channel_count);
  1746. spin_unlock_bh(&all_channels_lock);
  1747. return 0;
  1748. }
  1749. /*
  1750. * Return the index of a channel.
  1751. */
  1752. int ppp_channel_index(struct ppp_channel *chan)
  1753. {
  1754. struct channel *pch = chan->ppp;
  1755. if (pch != 0)
  1756. return pch->file.index;
  1757. return -1;
  1758. }
  1759. /*
  1760. * Return the PPP unit number to which a channel is connected.
  1761. */
  1762. int ppp_unit_number(struct ppp_channel *chan)
  1763. {
  1764. struct channel *pch = chan->ppp;
  1765. int unit = -1;
  1766. if (pch != 0) {
  1767. read_lock_bh(&pch->upl);
  1768. if (pch->ppp != 0)
  1769. unit = pch->ppp->file.index;
  1770. read_unlock_bh(&pch->upl);
  1771. }
  1772. return unit;
  1773. }
  1774. /*
  1775. * Disconnect a channel from the generic layer.
  1776. * This must be called in process context.
  1777. */
  1778. void
  1779. ppp_unregister_channel(struct ppp_channel *chan)
  1780. {
  1781. struct channel *pch = chan->ppp;
  1782. if (pch == 0)
  1783. return; /* should never happen */
  1784. chan->ppp = NULL;
  1785. /*
  1786. * This ensures that we have returned from any calls into the
  1787. * the channel's start_xmit or ioctl routine before we proceed.
  1788. */
  1789. down_write(&pch->chan_sem);
  1790. spin_lock_bh(&pch->downl);
  1791. pch->chan = NULL;
  1792. spin_unlock_bh(&pch->downl);
  1793. up_write(&pch->chan_sem);
  1794. ppp_disconnect_channel(pch);
  1795. spin_lock_bh(&all_channels_lock);
  1796. list_del(&pch->list);
  1797. spin_unlock_bh(&all_channels_lock);
  1798. pch->file.dead = 1;
  1799. wake_up_interruptible(&pch->file.rwait);
  1800. if (atomic_dec_and_test(&pch->file.refcnt))
  1801. ppp_destroy_channel(pch);
  1802. }
  1803. /*
  1804. * Callback from a channel when it can accept more to transmit.
  1805. * This should be called at BH/softirq level, not interrupt level.
  1806. */
  1807. void
  1808. ppp_output_wakeup(struct ppp_channel *chan)
  1809. {
  1810. struct channel *pch = chan->ppp;
  1811. if (pch == 0)
  1812. return;
  1813. ppp_channel_push(pch);
  1814. }
  1815. /*
  1816. * Compression control.
  1817. */
  1818. /* Process the PPPIOCSCOMPRESS ioctl. */
  1819. static int
  1820. ppp_set_compress(struct ppp *ppp, unsigned long arg)
  1821. {
  1822. int err;
  1823. struct compressor *cp, *ocomp;
  1824. struct ppp_option_data data;
  1825. void *state, *ostate;
  1826. unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
  1827. err = -EFAULT;
  1828. if (copy_from_user(&data, (void __user *) arg, sizeof(data))
  1829. || (data.length <= CCP_MAX_OPTION_LENGTH
  1830. && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
  1831. goto out;
  1832. err = -EINVAL;
  1833. if (data.length > CCP_MAX_OPTION_LENGTH
  1834. || ccp_option[1] < 2 || ccp_option[1] > data.length)
  1835. goto out;
  1836. cp = find_compressor(ccp_option[0]);
  1837. #ifdef CONFIG_KMOD
  1838. if (cp == 0) {
  1839. request_module("ppp-compress-%d", ccp_option[0]);
  1840. cp = find_compressor(ccp_option[0]);
  1841. }
  1842. #endif /* CONFIG_KMOD */
  1843. if (cp == 0)
  1844. goto out;
  1845. err = -ENOBUFS;
  1846. if (data.transmit) {
  1847. state = cp->comp_alloc(ccp_option, data.length);
  1848. if (state != 0) {
  1849. ppp_xmit_lock(ppp);
  1850. ppp->xstate &= ~SC_COMP_RUN;
  1851. ocomp = ppp->xcomp;
  1852. ostate = ppp->xc_state;
  1853. ppp->xcomp = cp;
  1854. ppp->xc_state = state;
  1855. ppp_xmit_unlock(ppp);
  1856. if (ostate != 0) {
  1857. ocomp->comp_free(ostate);
  1858. module_put(ocomp->owner);
  1859. }
  1860. err = 0;
  1861. } else
  1862. module_put(cp->owner);
  1863. } else {
  1864. state = cp->decomp_alloc(ccp_option, data.length);
  1865. if (state != 0) {
  1866. ppp_recv_lock(ppp);
  1867. ppp->rstate &= ~SC_DECOMP_RUN;
  1868. ocomp = ppp->rcomp;
  1869. ostate = ppp->rc_state;
  1870. ppp->rcomp = cp;
  1871. ppp->rc_state = state;
  1872. ppp_recv_unlock(ppp);
  1873. if (ostate != 0) {
  1874. ocomp->decomp_free(ostate);
  1875. module_put(ocomp->owner);
  1876. }
  1877. err = 0;
  1878. } else
  1879. module_put(cp->owner);
  1880. }
  1881. out:
  1882. return err;
  1883. }
  1884. /*
  1885. * Look at a CCP packet and update our state accordingly.
  1886. * We assume the caller has the xmit or recv path locked.
  1887. */
  1888. static void
  1889. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  1890. {
  1891. unsigned char *dp;
  1892. int len;
  1893. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  1894. return; /* no header */
  1895. dp = skb->data + 2;
  1896. switch (CCP_CODE(dp)) {
  1897. case CCP_CONFREQ:
  1898. /* A ConfReq starts negotiation of compression
  1899. * in one direction of transmission,
  1900. * and hence brings it down...but which way?
  1901. *
  1902. * Remember:
  1903. * A ConfReq indicates what the sender would like to receive
  1904. */
  1905. if(inbound)
  1906. /* He is proposing what I should send */
  1907. ppp->xstate &= ~SC_COMP_RUN;
  1908. else
  1909. /* I am proposing to what he should send */
  1910. ppp->rstate &= ~SC_DECOMP_RUN;
  1911. break;
  1912. case CCP_TERMREQ:
  1913. case CCP_TERMACK:
  1914. /*
  1915. * CCP is going down, both directions of transmission
  1916. */
  1917. ppp->rstate &= ~SC_DECOMP_RUN;
  1918. ppp->xstate &= ~SC_COMP_RUN;
  1919. break;
  1920. case CCP_CONFACK:
  1921. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  1922. break;
  1923. len = CCP_LENGTH(dp);
  1924. if (!pskb_may_pull(skb, len + 2))
  1925. return; /* too short */
  1926. dp += CCP_HDRLEN;
  1927. len -= CCP_HDRLEN;
  1928. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  1929. break;
  1930. if (inbound) {
  1931. /* we will start receiving compressed packets */
  1932. if (ppp->rc_state == 0)
  1933. break;
  1934. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  1935. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  1936. ppp->rstate |= SC_DECOMP_RUN;
  1937. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  1938. }
  1939. } else {
  1940. /* we will soon start sending compressed packets */
  1941. if (ppp->xc_state == 0)
  1942. break;
  1943. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  1944. ppp->file.index, 0, ppp->debug))
  1945. ppp->xstate |= SC_COMP_RUN;
  1946. }
  1947. break;
  1948. case CCP_RESETACK:
  1949. /* reset the [de]compressor */
  1950. if ((ppp->flags & SC_CCP_UP) == 0)
  1951. break;
  1952. if (inbound) {
  1953. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  1954. ppp->rcomp->decomp_reset(ppp->rc_state);
  1955. ppp->rstate &= ~SC_DC_ERROR;
  1956. }
  1957. } else {
  1958. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  1959. ppp->xcomp->comp_reset(ppp->xc_state);
  1960. }
  1961. break;
  1962. }
  1963. }
  1964. /* Free up compression resources. */
  1965. static void
  1966. ppp_ccp_closed(struct ppp *ppp)
  1967. {
  1968. void *xstate, *rstate;
  1969. struct compressor *xcomp, *rcomp;
  1970. ppp_lock(ppp);
  1971. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  1972. ppp->xstate = 0;
  1973. xcomp = ppp->xcomp;
  1974. xstate = ppp->xc_state;
  1975. ppp->xc_state = NULL;
  1976. ppp->rstate = 0;
  1977. rcomp = ppp->rcomp;
  1978. rstate = ppp->rc_state;
  1979. ppp->rc_state = NULL;
  1980. ppp_unlock(ppp);
  1981. if (xstate) {
  1982. xcomp->comp_free(xstate);
  1983. module_put(xcomp->owner);
  1984. }
  1985. if (rstate) {
  1986. rcomp->decomp_free(rstate);
  1987. module_put(rcomp->owner);
  1988. }
  1989. }
  1990. /* List of compressors. */
  1991. static LIST_HEAD(compressor_list);
  1992. static DEFINE_SPINLOCK(compressor_list_lock);
  1993. struct compressor_entry {
  1994. struct list_head list;
  1995. struct compressor *comp;
  1996. };
  1997. static struct compressor_entry *
  1998. find_comp_entry(int proto)
  1999. {
  2000. struct compressor_entry *ce;
  2001. struct list_head *list = &compressor_list;
  2002. while ((list = list->next) != &compressor_list) {
  2003. ce = list_entry(list, struct compressor_entry, list);
  2004. if (ce->comp->compress_proto == proto)
  2005. return ce;
  2006. }
  2007. return NULL;
  2008. }
  2009. /* Register a compressor */
  2010. int
  2011. ppp_register_compressor(struct compressor *cp)
  2012. {
  2013. struct compressor_entry *ce;
  2014. int ret;
  2015. spin_lock(&compressor_list_lock);
  2016. ret = -EEXIST;
  2017. if (find_comp_entry(cp->compress_proto) != 0)
  2018. goto out;
  2019. ret = -ENOMEM;
  2020. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2021. if (ce == 0)
  2022. goto out;
  2023. ret = 0;
  2024. ce->comp = cp;
  2025. list_add(&ce->list, &compressor_list);
  2026. out:
  2027. spin_unlock(&compressor_list_lock);
  2028. return ret;
  2029. }
  2030. /* Unregister a compressor */
  2031. void
  2032. ppp_unregister_compressor(struct compressor *cp)
  2033. {
  2034. struct compressor_entry *ce;
  2035. spin_lock(&compressor_list_lock);
  2036. ce = find_comp_entry(cp->compress_proto);
  2037. if (ce != 0 && ce->comp == cp) {
  2038. list_del(&ce->list);
  2039. kfree(ce);
  2040. }
  2041. spin_unlock(&compressor_list_lock);
  2042. }
  2043. /* Find a compressor. */
  2044. static struct compressor *
  2045. find_compressor(int type)
  2046. {
  2047. struct compressor_entry *ce;
  2048. struct compressor *cp = NULL;
  2049. spin_lock(&compressor_list_lock);
  2050. ce = find_comp_entry(type);
  2051. if (ce != 0) {
  2052. cp = ce->comp;
  2053. if (!try_module_get(cp->owner))
  2054. cp = NULL;
  2055. }
  2056. spin_unlock(&compressor_list_lock);
  2057. return cp;
  2058. }
  2059. /*
  2060. * Miscelleneous stuff.
  2061. */
  2062. static void
  2063. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2064. {
  2065. struct slcompress *vj = ppp->vj;
  2066. memset(st, 0, sizeof(*st));
  2067. st->p.ppp_ipackets = ppp->stats.rx_packets;
  2068. st->p.ppp_ierrors = ppp->stats.rx_errors;
  2069. st->p.ppp_ibytes = ppp->stats.rx_bytes;
  2070. st->p.ppp_opackets = ppp->stats.tx_packets;
  2071. st->p.ppp_oerrors = ppp->stats.tx_errors;
  2072. st->p.ppp_obytes = ppp->stats.tx_bytes;
  2073. if (vj == 0)
  2074. return;
  2075. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2076. st->vj.vjs_compressed = vj->sls_o_compressed;
  2077. st->vj.vjs_searches = vj->sls_o_searches;
  2078. st->vj.vjs_misses = vj->sls_o_misses;
  2079. st->vj.vjs_errorin = vj->sls_i_error;
  2080. st->vj.vjs_tossed = vj->sls_i_tossed;
  2081. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2082. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2083. }
  2084. /*
  2085. * Stuff for handling the lists of ppp units and channels
  2086. * and for initialization.
  2087. */
  2088. /*
  2089. * Create a new ppp interface unit. Fails if it can't allocate memory
  2090. * or if there is already a unit with the requested number.
  2091. * unit == -1 means allocate a new number.
  2092. */
  2093. static struct ppp *
  2094. ppp_create_interface(int unit, int *retp)
  2095. {
  2096. struct ppp *ppp;
  2097. struct net_device *dev = NULL;
  2098. int ret = -ENOMEM;
  2099. int i;
  2100. ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
  2101. if (!ppp)
  2102. goto out;
  2103. dev = alloc_netdev(0, "", ppp_setup);
  2104. if (!dev)
  2105. goto out1;
  2106. memset(ppp, 0, sizeof(struct ppp));
  2107. ppp->mru = PPP_MRU;
  2108. init_ppp_file(&ppp->file, INTERFACE);
  2109. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  2110. for (i = 0; i < NUM_NP; ++i)
  2111. ppp->npmode[i] = NPMODE_PASS;
  2112. INIT_LIST_HEAD(&ppp->channels);
  2113. spin_lock_init(&ppp->rlock);
  2114. spin_lock_init(&ppp->wlock);
  2115. #ifdef CONFIG_PPP_MULTILINK
  2116. ppp->minseq = -1;
  2117. skb_queue_head_init(&ppp->mrq);
  2118. #endif /* CONFIG_PPP_MULTILINK */
  2119. ppp->dev = dev;
  2120. dev->priv = ppp;
  2121. dev->hard_start_xmit = ppp_start_xmit;
  2122. dev->get_stats = ppp_net_stats;
  2123. dev->do_ioctl = ppp_net_ioctl;
  2124. ret = -EEXIST;
  2125. down(&all_ppp_sem);
  2126. if (unit < 0)
  2127. unit = cardmap_find_first_free(all_ppp_units);
  2128. else if (cardmap_get(all_ppp_units, unit) != NULL)
  2129. goto out2; /* unit already exists */
  2130. /* Initialize the new ppp unit */
  2131. ppp->file.index = unit;
  2132. sprintf(dev->name, "ppp%d", unit);
  2133. ret = register_netdev(dev);
  2134. if (ret != 0) {
  2135. printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
  2136. dev->name, ret);
  2137. goto out2;
  2138. }
  2139. atomic_inc(&ppp_unit_count);
  2140. cardmap_set(&all_ppp_units, unit, ppp);
  2141. up(&all_ppp_sem);
  2142. *retp = 0;
  2143. return ppp;
  2144. out2:
  2145. up(&all_ppp_sem);
  2146. free_netdev(dev);
  2147. out1:
  2148. kfree(ppp);
  2149. out:
  2150. *retp = ret;
  2151. return NULL;
  2152. }
  2153. /*
  2154. * Initialize a ppp_file structure.
  2155. */
  2156. static void
  2157. init_ppp_file(struct ppp_file *pf, int kind)
  2158. {
  2159. pf->kind = kind;
  2160. skb_queue_head_init(&pf->xq);
  2161. skb_queue_head_init(&pf->rq);
  2162. atomic_set(&pf->refcnt, 1);
  2163. init_waitqueue_head(&pf->rwait);
  2164. }
  2165. /*
  2166. * Take down a ppp interface unit - called when the owning file
  2167. * (the one that created the unit) is closed or detached.
  2168. */
  2169. static void ppp_shutdown_interface(struct ppp *ppp)
  2170. {
  2171. struct net_device *dev;
  2172. down(&all_ppp_sem);
  2173. ppp_lock(ppp);
  2174. dev = ppp->dev;
  2175. ppp->dev = NULL;
  2176. ppp_unlock(ppp);
  2177. /* This will call dev_close() for us. */
  2178. if (dev) {
  2179. unregister_netdev(dev);
  2180. free_netdev(dev);
  2181. }
  2182. cardmap_set(&all_ppp_units, ppp->file.index, NULL);
  2183. ppp->file.dead = 1;
  2184. ppp->owner = NULL;
  2185. wake_up_interruptible(&ppp->file.rwait);
  2186. up(&all_ppp_sem);
  2187. }
  2188. /*
  2189. * Free the memory used by a ppp unit. This is only called once
  2190. * there are no channels connected to the unit and no file structs
  2191. * that reference the unit.
  2192. */
  2193. static void ppp_destroy_interface(struct ppp *ppp)
  2194. {
  2195. atomic_dec(&ppp_unit_count);
  2196. if (!ppp->file.dead || ppp->n_channels) {
  2197. /* "can't happen" */
  2198. printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
  2199. "n_channels=%d !\n", ppp, ppp->file.dead,
  2200. ppp->n_channels);
  2201. return;
  2202. }
  2203. ppp_ccp_closed(ppp);
  2204. if (ppp->vj) {
  2205. slhc_free(ppp->vj);
  2206. ppp->vj = NULL;
  2207. }
  2208. skb_queue_purge(&ppp->file.xq);
  2209. skb_queue_purge(&ppp->file.rq);
  2210. #ifdef CONFIG_PPP_MULTILINK
  2211. skb_queue_purge(&ppp->mrq);
  2212. #endif /* CONFIG_PPP_MULTILINK */
  2213. #ifdef CONFIG_PPP_FILTER
  2214. if (ppp->pass_filter) {
  2215. kfree(ppp->pass_filter);
  2216. ppp->pass_filter = NULL;
  2217. }
  2218. if (ppp->active_filter) {
  2219. kfree(ppp->active_filter);
  2220. ppp->active_filter = NULL;
  2221. }
  2222. #endif /* CONFIG_PPP_FILTER */
  2223. kfree(ppp);
  2224. }
  2225. /*
  2226. * Locate an existing ppp unit.
  2227. * The caller should have locked the all_ppp_sem.
  2228. */
  2229. static struct ppp *
  2230. ppp_find_unit(int unit)
  2231. {
  2232. return cardmap_get(all_ppp_units, unit);
  2233. }
  2234. /*
  2235. * Locate an existing ppp channel.
  2236. * The caller should have locked the all_channels_lock.
  2237. * First we look in the new_channels list, then in the
  2238. * all_channels list. If found in the new_channels list,
  2239. * we move it to the all_channels list. This is for speed
  2240. * when we have a lot of channels in use.
  2241. */
  2242. static struct channel *
  2243. ppp_find_channel(int unit)
  2244. {
  2245. struct channel *pch;
  2246. struct list_head *list;
  2247. list = &new_channels;
  2248. while ((list = list->next) != &new_channels) {
  2249. pch = list_entry(list, struct channel, list);
  2250. if (pch->file.index == unit) {
  2251. list_del(&pch->list);
  2252. list_add(&pch->list, &all_channels);
  2253. return pch;
  2254. }
  2255. }
  2256. list = &all_channels;
  2257. while ((list = list->next) != &all_channels) {
  2258. pch = list_entry(list, struct channel, list);
  2259. if (pch->file.index == unit)
  2260. return pch;
  2261. }
  2262. return NULL;
  2263. }
  2264. /*
  2265. * Connect a PPP channel to a PPP interface unit.
  2266. */
  2267. static int
  2268. ppp_connect_channel(struct channel *pch, int unit)
  2269. {
  2270. struct ppp *ppp;
  2271. int ret = -ENXIO;
  2272. int hdrlen;
  2273. down(&all_ppp_sem);
  2274. ppp = ppp_find_unit(unit);
  2275. if (ppp == 0)
  2276. goto out;
  2277. write_lock_bh(&pch->upl);
  2278. ret = -EINVAL;
  2279. if (pch->ppp != 0)
  2280. goto outl;
  2281. ppp_lock(ppp);
  2282. if (pch->file.hdrlen > ppp->file.hdrlen)
  2283. ppp->file.hdrlen = pch->file.hdrlen;
  2284. hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
  2285. if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
  2286. ppp->dev->hard_header_len = hdrlen;
  2287. list_add_tail(&pch->clist, &ppp->channels);
  2288. ++ppp->n_channels;
  2289. pch->ppp = ppp;
  2290. atomic_inc(&ppp->file.refcnt);
  2291. ppp_unlock(ppp);
  2292. ret = 0;
  2293. outl:
  2294. write_unlock_bh(&pch->upl);
  2295. out:
  2296. up(&all_ppp_sem);
  2297. return ret;
  2298. }
  2299. /*
  2300. * Disconnect a channel from its ppp unit.
  2301. */
  2302. static int
  2303. ppp_disconnect_channel(struct channel *pch)
  2304. {
  2305. struct ppp *ppp;
  2306. int err = -EINVAL;
  2307. write_lock_bh(&pch->upl);
  2308. ppp = pch->ppp;
  2309. pch->ppp = NULL;
  2310. write_unlock_bh(&pch->upl);
  2311. if (ppp != 0) {
  2312. /* remove it from the ppp unit's list */
  2313. ppp_lock(ppp);
  2314. list_del(&pch->clist);
  2315. if (--ppp->n_channels == 0)
  2316. wake_up_interruptible(&ppp->file.rwait);
  2317. ppp_unlock(ppp);
  2318. if (atomic_dec_and_test(&ppp->file.refcnt))
  2319. ppp_destroy_interface(ppp);
  2320. err = 0;
  2321. }
  2322. return err;
  2323. }
  2324. /*
  2325. * Free up the resources used by a ppp channel.
  2326. */
  2327. static void ppp_destroy_channel(struct channel *pch)
  2328. {
  2329. atomic_dec(&channel_count);
  2330. if (!pch->file.dead) {
  2331. /* "can't happen" */
  2332. printk(KERN_ERR "ppp: destroying undead channel %p !\n",
  2333. pch);
  2334. return;
  2335. }
  2336. skb_queue_purge(&pch->file.xq);
  2337. skb_queue_purge(&pch->file.rq);
  2338. kfree(pch);
  2339. }
  2340. static void __exit ppp_cleanup(void)
  2341. {
  2342. /* should never happen */
  2343. if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
  2344. printk(KERN_ERR "PPP: removing module but units remain!\n");
  2345. cardmap_destroy(&all_ppp_units);
  2346. if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
  2347. printk(KERN_ERR "PPP: failed to unregister PPP device\n");
  2348. devfs_remove("ppp");
  2349. class_simple_device_remove(MKDEV(PPP_MAJOR, 0));
  2350. class_simple_destroy(ppp_class);
  2351. }
  2352. /*
  2353. * Cardmap implementation.
  2354. */
  2355. static void *cardmap_get(struct cardmap *map, unsigned int nr)
  2356. {
  2357. struct cardmap *p;
  2358. int i;
  2359. for (p = map; p != NULL; ) {
  2360. if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
  2361. return NULL;
  2362. if (p->shift == 0)
  2363. return p->ptr[i];
  2364. nr &= ~(CARDMAP_MASK << p->shift);
  2365. p = p->ptr[i];
  2366. }
  2367. return NULL;
  2368. }
  2369. static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
  2370. {
  2371. struct cardmap *p;
  2372. int i;
  2373. p = *pmap;
  2374. if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
  2375. do {
  2376. /* need a new top level */
  2377. struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
  2378. memset(np, 0, sizeof(*np));
  2379. np->ptr[0] = p;
  2380. if (p != NULL) {
  2381. np->shift = p->shift + CARDMAP_ORDER;
  2382. p->parent = np;
  2383. } else
  2384. np->shift = 0;
  2385. p = np;
  2386. } while ((nr >> p->shift) >= CARDMAP_WIDTH);
  2387. *pmap = p;
  2388. }
  2389. while (p->shift > 0) {
  2390. i = (nr >> p->shift) & CARDMAP_MASK;
  2391. if (p->ptr[i] == NULL) {
  2392. struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
  2393. memset(np, 0, sizeof(*np));
  2394. np->shift = p->shift - CARDMAP_ORDER;
  2395. np->parent = p;
  2396. p->ptr[i] = np;
  2397. }
  2398. if (ptr == NULL)
  2399. clear_bit(i, &p->inuse);
  2400. p = p->ptr[i];
  2401. }
  2402. i = nr & CARDMAP_MASK;
  2403. p->ptr[i] = ptr;
  2404. if (ptr != NULL)
  2405. set_bit(i, &p->inuse);
  2406. else
  2407. clear_bit(i, &p->inuse);
  2408. }
  2409. static unsigned int cardmap_find_first_free(struct cardmap *map)
  2410. {
  2411. struct cardmap *p;
  2412. unsigned int nr = 0;
  2413. int i;
  2414. if ((p = map) == NULL)
  2415. return 0;
  2416. for (;;) {
  2417. i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
  2418. if (i >= CARDMAP_WIDTH) {
  2419. if (p->parent == NULL)
  2420. return CARDMAP_WIDTH << p->shift;
  2421. p = p->parent;
  2422. i = (nr >> p->shift) & CARDMAP_MASK;
  2423. set_bit(i, &p->inuse);
  2424. continue;
  2425. }
  2426. nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
  2427. if (p->shift == 0 || p->ptr[i] == NULL)
  2428. return nr;
  2429. p = p->ptr[i];
  2430. }
  2431. }
  2432. static void cardmap_destroy(struct cardmap **pmap)
  2433. {
  2434. struct cardmap *p, *np;
  2435. int i;
  2436. for (p = *pmap; p != NULL; p = np) {
  2437. if (p->shift != 0) {
  2438. for (i = 0; i < CARDMAP_WIDTH; ++i)
  2439. if (p->ptr[i] != NULL)
  2440. break;
  2441. if (i < CARDMAP_WIDTH) {
  2442. np = p->ptr[i];
  2443. p->ptr[i] = NULL;
  2444. continue;
  2445. }
  2446. }
  2447. np = p->parent;
  2448. kfree(p);
  2449. }
  2450. *pmap = NULL;
  2451. }
  2452. /* Module/initialization stuff */
  2453. module_init(ppp_init);
  2454. module_exit(ppp_cleanup);
  2455. EXPORT_SYMBOL(ppp_register_channel);
  2456. EXPORT_SYMBOL(ppp_unregister_channel);
  2457. EXPORT_SYMBOL(ppp_channel_index);
  2458. EXPORT_SYMBOL(ppp_unit_number);
  2459. EXPORT_SYMBOL(ppp_input);
  2460. EXPORT_SYMBOL(ppp_input_error);
  2461. EXPORT_SYMBOL(ppp_output_wakeup);
  2462. EXPORT_SYMBOL(ppp_register_compressor);
  2463. EXPORT_SYMBOL(ppp_unregister_compressor);
  2464. MODULE_LICENSE("GPL");
  2465. MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
  2466. MODULE_ALIAS("/dev/ppp");