pppoe.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /** -*- linux-c -*- ***********************************************************
  2. * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
  3. *
  4. * PPPoX --- Generic PPP encapsulation socket family
  5. * PPPoE --- PPP over Ethernet (RFC 2516)
  6. *
  7. *
  8. * Version: 0.7.0
  9. *
  10. * 070228 : Fix to allow multiple sessions with same remote MAC and same
  11. * session id by including the local device ifindex in the
  12. * tuple identifying a session. This also ensures packets can't
  13. * be injected into a session from interfaces other than the one
  14. * specified by userspace. Florian Zumbiehl <florz@florz.de>
  15. * (Oh, BTW, this one is YYMMDD, in case you were wondering ...)
  16. * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme
  17. * 030700 : Fixed connect logic to allow for disconnect.
  18. * 270700 : Fixed potential SMP problems; we must protect against
  19. * simultaneous invocation of ppp_input
  20. * and ppp_unregister_channel.
  21. * 040800 : Respect reference count mechanisms on net-devices.
  22. * 200800 : fix kfree(skb) in pppoe_rcv (acme)
  23. * Module reference count is decremented in the right spot now,
  24. * guards against sock_put not actually freeing the sk
  25. * in pppoe_release.
  26. * 051000 : Initialization cleanup.
  27. * 111100 : Fix recvmsg.
  28. * 050101 : Fix PADT procesing.
  29. * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey)
  30. * 170701 : Do not lock_sock with rwlock held. (DaveM)
  31. * Ignore discovery frames if user has socket
  32. * locked. (DaveM)
  33. * Ignore return value of dev_queue_xmit in __pppoe_xmit
  34. * or else we may kfree an SKB twice. (DaveM)
  35. * 190701 : When doing copies of skb's in __pppoe_xmit, always delete
  36. * the original skb that was passed in on success, never on
  37. * failure. Delete the copy of the skb on failure to avoid
  38. * a memory leak.
  39. * 081001 : Misc. cleanup (licence string, non-blocking, prevent
  40. * reference of device on close).
  41. * 121301 : New ppp channels interface; cannot unregister a channel
  42. * from interrupts. Thus, we mark the socket as a ZOMBIE
  43. * and do the unregistration later.
  44. * 081002 : seq_file support for proc stuff -acme
  45. * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6
  46. * as version 0.7. Spacing cleanup.
  47. * Author: Michal Ostrowski <mostrows@speakeasy.net>
  48. * Contributors:
  49. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  50. * David S. Miller (davem@redhat.com)
  51. *
  52. * License:
  53. * This program is free software; you can redistribute it and/or
  54. * modify it under the terms of the GNU General Public License
  55. * as published by the Free Software Foundation; either version
  56. * 2 of the License, or (at your option) any later version.
  57. *
  58. */
  59. #include <linux/string.h>
  60. #include <linux/module.h>
  61. #include <linux/kernel.h>
  62. #include <linux/slab.h>
  63. #include <linux/errno.h>
  64. #include <linux/netdevice.h>
  65. #include <linux/net.h>
  66. #include <linux/inetdevice.h>
  67. #include <linux/etherdevice.h>
  68. #include <linux/skbuff.h>
  69. #include <linux/init.h>
  70. #include <linux/if_ether.h>
  71. #include <linux/if_pppox.h>
  72. #include <linux/ppp_channel.h>
  73. #include <linux/ppp_defs.h>
  74. #include <linux/if_ppp.h>
  75. #include <linux/notifier.h>
  76. #include <linux/file.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/seq_file.h>
  79. #include <net/sock.h>
  80. #include <asm/uaccess.h>
  81. #define PPPOE_HASH_BITS 4
  82. #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
  83. static struct ppp_channel_ops pppoe_chan_ops;
  84. static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
  85. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
  86. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
  87. static const struct proto_ops pppoe_ops;
  88. static DEFINE_RWLOCK(pppoe_hash_lock);
  89. static struct ppp_channel_ops pppoe_chan_ops;
  90. static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
  91. {
  92. return (a->sid == b->sid &&
  93. (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
  94. }
  95. static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
  96. {
  97. return (a->sid == sid &&
  98. (memcmp(a->remote,addr,ETH_ALEN) == 0));
  99. }
  100. #if 8%PPPOE_HASH_BITS
  101. #error 8 must be a multiple of PPPOE_HASH_BITS
  102. #endif
  103. static int hash_item(unsigned int sid, unsigned char *addr)
  104. {
  105. unsigned char hash = 0;
  106. unsigned int i;
  107. for (i = 0 ; i < ETH_ALEN ; i++) {
  108. hash ^= addr[i];
  109. }
  110. for (i = 0 ; i < sizeof(sid_t)*8 ; i += 8 ){
  111. hash ^= sid>>i;
  112. }
  113. for (i = 8 ; (i>>=1) >= PPPOE_HASH_BITS ; ) {
  114. hash ^= hash>>i;
  115. }
  116. return hash & ( PPPOE_HASH_SIZE - 1 );
  117. }
  118. /* zeroed because its in .bss */
  119. static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
  120. /**********************************************************************
  121. *
  122. * Set/get/delete/rehash items (internal versions)
  123. *
  124. **********************************************************************/
  125. static struct pppox_sock *__get_item(unsigned long sid, unsigned char *addr, int ifindex)
  126. {
  127. int hash = hash_item(sid, addr);
  128. struct pppox_sock *ret;
  129. ret = item_hash_table[hash];
  130. while (ret && !(cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex))
  131. ret = ret->next;
  132. return ret;
  133. }
  134. static int __set_item(struct pppox_sock *po)
  135. {
  136. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  137. struct pppox_sock *ret;
  138. ret = item_hash_table[hash];
  139. while (ret) {
  140. if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) && ret->pppoe_ifindex == po->pppoe_ifindex)
  141. return -EALREADY;
  142. ret = ret->next;
  143. }
  144. po->next = item_hash_table[hash];
  145. item_hash_table[hash] = po;
  146. return 0;
  147. }
  148. static struct pppox_sock *__delete_item(unsigned long sid, char *addr, int ifindex)
  149. {
  150. int hash = hash_item(sid, addr);
  151. struct pppox_sock *ret, **src;
  152. ret = item_hash_table[hash];
  153. src = &item_hash_table[hash];
  154. while (ret) {
  155. if (cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex) {
  156. *src = ret->next;
  157. break;
  158. }
  159. src = &ret->next;
  160. ret = ret->next;
  161. }
  162. return ret;
  163. }
  164. /**********************************************************************
  165. *
  166. * Set/get/delete/rehash items
  167. *
  168. **********************************************************************/
  169. static inline struct pppox_sock *get_item(unsigned long sid,
  170. unsigned char *addr, int ifindex)
  171. {
  172. struct pppox_sock *po;
  173. read_lock_bh(&pppoe_hash_lock);
  174. po = __get_item(sid, addr, ifindex);
  175. if (po)
  176. sock_hold(sk_pppox(po));
  177. read_unlock_bh(&pppoe_hash_lock);
  178. return po;
  179. }
  180. static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp)
  181. {
  182. struct net_device *dev;
  183. int ifindex;
  184. dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
  185. if(!dev)
  186. return NULL;
  187. ifindex = dev->ifindex;
  188. dev_put(dev);
  189. return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote, ifindex);
  190. }
  191. static inline struct pppox_sock *delete_item(unsigned long sid, char *addr, int ifindex)
  192. {
  193. struct pppox_sock *ret;
  194. write_lock_bh(&pppoe_hash_lock);
  195. ret = __delete_item(sid, addr, ifindex);
  196. write_unlock_bh(&pppoe_hash_lock);
  197. return ret;
  198. }
  199. /***************************************************************************
  200. *
  201. * Handler for device events.
  202. * Certain device events require that sockets be unconnected.
  203. *
  204. **************************************************************************/
  205. static void pppoe_flush_dev(struct net_device *dev)
  206. {
  207. int hash;
  208. BUG_ON(dev == NULL);
  209. write_lock_bh(&pppoe_hash_lock);
  210. for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
  211. struct pppox_sock *po = item_hash_table[hash];
  212. while (po != NULL) {
  213. struct sock *sk = sk_pppox(po);
  214. if (po->pppoe_dev != dev) {
  215. po = po->next;
  216. continue;
  217. }
  218. po->pppoe_dev = NULL;
  219. dev_put(dev);
  220. /* We always grab the socket lock, followed by the
  221. * pppoe_hash_lock, in that order. Since we should
  222. * hold the sock lock while doing any unbinding,
  223. * we need to release the lock we're holding.
  224. * Hold a reference to the sock so it doesn't disappear
  225. * as we're jumping between locks.
  226. */
  227. sock_hold(sk);
  228. write_unlock_bh(&pppoe_hash_lock);
  229. lock_sock(sk);
  230. if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
  231. pppox_unbind_sock(sk);
  232. sk->sk_state = PPPOX_ZOMBIE;
  233. sk->sk_state_change(sk);
  234. }
  235. release_sock(sk);
  236. sock_put(sk);
  237. /* Restart scan at the beginning of this hash chain.
  238. * While the lock was dropped the chain contents may
  239. * have changed.
  240. */
  241. write_lock_bh(&pppoe_hash_lock);
  242. po = item_hash_table[hash];
  243. }
  244. }
  245. write_unlock_bh(&pppoe_hash_lock);
  246. }
  247. static int pppoe_device_event(struct notifier_block *this,
  248. unsigned long event, void *ptr)
  249. {
  250. struct net_device *dev = (struct net_device *) ptr;
  251. /* Only look at sockets that are using this specific device. */
  252. switch (event) {
  253. case NETDEV_CHANGEMTU:
  254. /* A change in mtu is a bad thing, requiring
  255. * LCP re-negotiation.
  256. */
  257. case NETDEV_GOING_DOWN:
  258. case NETDEV_DOWN:
  259. /* Find every socket on this device and kill it. */
  260. pppoe_flush_dev(dev);
  261. break;
  262. default:
  263. break;
  264. };
  265. return NOTIFY_DONE;
  266. }
  267. static struct notifier_block pppoe_notifier = {
  268. .notifier_call = pppoe_device_event,
  269. };
  270. /************************************************************************
  271. *
  272. * Do the real work of receiving a PPPoE Session frame.
  273. *
  274. ***********************************************************************/
  275. static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
  276. {
  277. struct pppox_sock *po = pppox_sk(sk);
  278. struct pppox_sock *relay_po;
  279. if (sk->sk_state & PPPOX_BOUND) {
  280. struct pppoe_hdr *ph = pppoe_hdr(skb);
  281. int len = ntohs(ph->length);
  282. skb_pull_rcsum(skb, sizeof(struct pppoe_hdr));
  283. if (pskb_trim_rcsum(skb, len))
  284. goto abort_kfree;
  285. ppp_input(&po->chan, skb);
  286. } else if (sk->sk_state & PPPOX_RELAY) {
  287. relay_po = get_item_by_addr(&po->pppoe_relay);
  288. if (relay_po == NULL)
  289. goto abort_kfree;
  290. if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
  291. goto abort_put;
  292. skb_pull(skb, sizeof(struct pppoe_hdr));
  293. if (!__pppoe_xmit(sk_pppox(relay_po), skb))
  294. goto abort_put;
  295. } else {
  296. if (sock_queue_rcv_skb(sk, skb))
  297. goto abort_kfree;
  298. }
  299. return NET_RX_SUCCESS;
  300. abort_put:
  301. sock_put(sk_pppox(relay_po));
  302. abort_kfree:
  303. kfree_skb(skb);
  304. return NET_RX_DROP;
  305. }
  306. /************************************************************************
  307. *
  308. * Receive wrapper called in BH context.
  309. *
  310. ***********************************************************************/
  311. static int pppoe_rcv(struct sk_buff *skb,
  312. struct net_device *dev,
  313. struct packet_type *pt,
  314. struct net_device *orig_dev)
  315. {
  316. struct pppoe_hdr *ph;
  317. struct pppox_sock *po;
  318. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  319. goto drop;
  320. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  321. goto out;
  322. ph = pppoe_hdr(skb);
  323. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  324. if (po != NULL)
  325. return sk_receive_skb(sk_pppox(po), skb, 0);
  326. drop:
  327. kfree_skb(skb);
  328. out:
  329. return NET_RX_DROP;
  330. }
  331. /************************************************************************
  332. *
  333. * Receive a PPPoE Discovery frame.
  334. * This is solely for detection of PADT frames
  335. *
  336. ***********************************************************************/
  337. static int pppoe_disc_rcv(struct sk_buff *skb,
  338. struct net_device *dev,
  339. struct packet_type *pt,
  340. struct net_device *orig_dev)
  341. {
  342. struct pppoe_hdr *ph;
  343. struct pppox_sock *po;
  344. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  345. goto abort;
  346. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  347. goto out;
  348. ph = pppoe_hdr(skb);
  349. if (ph->code != PADT_CODE)
  350. goto abort;
  351. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
  352. if (po) {
  353. struct sock *sk = sk_pppox(po);
  354. bh_lock_sock(sk);
  355. /* If the user has locked the socket, just ignore
  356. * the packet. With the way two rcv protocols hook into
  357. * one socket family type, we cannot (easily) distinguish
  358. * what kind of SKB it is during backlog rcv.
  359. */
  360. if (sock_owned_by_user(sk) == 0) {
  361. /* We're no longer connect at the PPPOE layer,
  362. * and must wait for ppp channel to disconnect us.
  363. */
  364. sk->sk_state = PPPOX_ZOMBIE;
  365. }
  366. bh_unlock_sock(sk);
  367. sock_put(sk);
  368. }
  369. abort:
  370. kfree_skb(skb);
  371. out:
  372. return NET_RX_SUCCESS; /* Lies... :-) */
  373. }
  374. static struct packet_type pppoes_ptype = {
  375. .type = __constant_htons(ETH_P_PPP_SES),
  376. .func = pppoe_rcv,
  377. };
  378. static struct packet_type pppoed_ptype = {
  379. .type = __constant_htons(ETH_P_PPP_DISC),
  380. .func = pppoe_disc_rcv,
  381. };
  382. static struct proto pppoe_sk_proto = {
  383. .name = "PPPOE",
  384. .owner = THIS_MODULE,
  385. .obj_size = sizeof(struct pppox_sock),
  386. };
  387. /***********************************************************************
  388. *
  389. * Initialize a new struct sock.
  390. *
  391. **********************************************************************/
  392. static int pppoe_create(struct socket *sock)
  393. {
  394. int error = -ENOMEM;
  395. struct sock *sk;
  396. sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
  397. if (!sk)
  398. goto out;
  399. sock_init_data(sock, sk);
  400. sock->state = SS_UNCONNECTED;
  401. sock->ops = &pppoe_ops;
  402. sk->sk_backlog_rcv = pppoe_rcv_core;
  403. sk->sk_state = PPPOX_NONE;
  404. sk->sk_type = SOCK_STREAM;
  405. sk->sk_family = PF_PPPOX;
  406. sk->sk_protocol = PX_PROTO_OE;
  407. error = 0;
  408. out: return error;
  409. }
  410. static int pppoe_release(struct socket *sock)
  411. {
  412. struct sock *sk = sock->sk;
  413. struct pppox_sock *po;
  414. if (!sk)
  415. return 0;
  416. lock_sock(sk);
  417. if (sock_flag(sk, SOCK_DEAD)){
  418. release_sock(sk);
  419. return -EBADF;
  420. }
  421. pppox_unbind_sock(sk);
  422. /* Signal the death of the socket. */
  423. sk->sk_state = PPPOX_DEAD;
  424. /* Write lock on hash lock protects the entire "po" struct from
  425. * concurrent updates via pppoe_flush_dev. The "po" struct should
  426. * be considered part of the hash table contents, thus protected
  427. * by the hash table lock */
  428. write_lock_bh(&pppoe_hash_lock);
  429. po = pppox_sk(sk);
  430. if (po->pppoe_pa.sid) {
  431. __delete_item(po->pppoe_pa.sid,
  432. po->pppoe_pa.remote, po->pppoe_ifindex);
  433. }
  434. if (po->pppoe_dev) {
  435. dev_put(po->pppoe_dev);
  436. po->pppoe_dev = NULL;
  437. }
  438. write_unlock_bh(&pppoe_hash_lock);
  439. sock_orphan(sk);
  440. sock->sk = NULL;
  441. skb_queue_purge(&sk->sk_receive_queue);
  442. release_sock(sk);
  443. sock_put(sk);
  444. return 0;
  445. }
  446. static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
  447. int sockaddr_len, int flags)
  448. {
  449. struct sock *sk = sock->sk;
  450. struct net_device *dev;
  451. struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
  452. struct pppox_sock *po = pppox_sk(sk);
  453. int error;
  454. lock_sock(sk);
  455. error = -EINVAL;
  456. if (sp->sa_protocol != PX_PROTO_OE)
  457. goto end;
  458. /* Check for already bound sockets */
  459. error = -EBUSY;
  460. if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
  461. goto end;
  462. /* Check for already disconnected sockets, on attempts to disconnect */
  463. error = -EALREADY;
  464. if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
  465. goto end;
  466. error = 0;
  467. if (po->pppoe_pa.sid) {
  468. pppox_unbind_sock(sk);
  469. /* Delete the old binding */
  470. delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote,po->pppoe_ifindex);
  471. if(po->pppoe_dev)
  472. dev_put(po->pppoe_dev);
  473. memset(sk_pppox(po) + 1, 0,
  474. sizeof(struct pppox_sock) - sizeof(struct sock));
  475. sk->sk_state = PPPOX_NONE;
  476. }
  477. /* Don't re-bind if sid==0 */
  478. if (sp->sa_addr.pppoe.sid != 0) {
  479. dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
  480. error = -ENODEV;
  481. if (!dev)
  482. goto end;
  483. po->pppoe_dev = dev;
  484. po->pppoe_ifindex = dev->ifindex;
  485. write_lock_bh(&pppoe_hash_lock);
  486. if (!(dev->flags & IFF_UP)){
  487. write_unlock_bh(&pppoe_hash_lock);
  488. goto err_put;
  489. }
  490. memcpy(&po->pppoe_pa,
  491. &sp->sa_addr.pppoe,
  492. sizeof(struct pppoe_addr));
  493. error = __set_item(po);
  494. write_unlock_bh(&pppoe_hash_lock);
  495. if (error < 0)
  496. goto err_put;
  497. po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
  498. dev->hard_header_len);
  499. po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr);
  500. po->chan.private = sk;
  501. po->chan.ops = &pppoe_chan_ops;
  502. error = ppp_register_channel(&po->chan);
  503. if (error)
  504. goto err_put;
  505. sk->sk_state = PPPOX_CONNECTED;
  506. }
  507. po->num = sp->sa_addr.pppoe.sid;
  508. end:
  509. release_sock(sk);
  510. return error;
  511. err_put:
  512. if (po->pppoe_dev) {
  513. dev_put(po->pppoe_dev);
  514. po->pppoe_dev = NULL;
  515. }
  516. goto end;
  517. }
  518. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  519. int *usockaddr_len, int peer)
  520. {
  521. int len = sizeof(struct sockaddr_pppox);
  522. struct sockaddr_pppox sp;
  523. sp.sa_family = AF_PPPOX;
  524. sp.sa_protocol = PX_PROTO_OE;
  525. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  526. sizeof(struct pppoe_addr));
  527. memcpy(uaddr, &sp, len);
  528. *usockaddr_len = len;
  529. return 0;
  530. }
  531. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  532. unsigned long arg)
  533. {
  534. struct sock *sk = sock->sk;
  535. struct pppox_sock *po = pppox_sk(sk);
  536. int val;
  537. int err;
  538. switch (cmd) {
  539. case PPPIOCGMRU:
  540. err = -ENXIO;
  541. if (!(sk->sk_state & PPPOX_CONNECTED))
  542. break;
  543. err = -EFAULT;
  544. if (put_user(po->pppoe_dev->mtu -
  545. sizeof(struct pppoe_hdr) -
  546. PPP_HDRLEN,
  547. (int __user *) arg))
  548. break;
  549. err = 0;
  550. break;
  551. case PPPIOCSMRU:
  552. err = -ENXIO;
  553. if (!(sk->sk_state & PPPOX_CONNECTED))
  554. break;
  555. err = -EFAULT;
  556. if (get_user(val,(int __user *) arg))
  557. break;
  558. if (val < (po->pppoe_dev->mtu
  559. - sizeof(struct pppoe_hdr)
  560. - PPP_HDRLEN))
  561. err = 0;
  562. else
  563. err = -EINVAL;
  564. break;
  565. case PPPIOCSFLAGS:
  566. err = -EFAULT;
  567. if (get_user(val, (int __user *) arg))
  568. break;
  569. err = 0;
  570. break;
  571. case PPPOEIOCSFWD:
  572. {
  573. struct pppox_sock *relay_po;
  574. err = -EBUSY;
  575. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  576. break;
  577. err = -ENOTCONN;
  578. if (!(sk->sk_state & PPPOX_CONNECTED))
  579. break;
  580. /* PPPoE address from the user specifies an outbound
  581. PPPoE address which frames are forwarded to */
  582. err = -EFAULT;
  583. if (copy_from_user(&po->pppoe_relay,
  584. (void __user *)arg,
  585. sizeof(struct sockaddr_pppox)))
  586. break;
  587. err = -EINVAL;
  588. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  589. po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
  590. break;
  591. /* Check that the socket referenced by the address
  592. actually exists. */
  593. relay_po = get_item_by_addr(&po->pppoe_relay);
  594. if (!relay_po)
  595. break;
  596. sock_put(sk_pppox(relay_po));
  597. sk->sk_state |= PPPOX_RELAY;
  598. err = 0;
  599. break;
  600. }
  601. case PPPOEIOCDFWD:
  602. err = -EALREADY;
  603. if (!(sk->sk_state & PPPOX_RELAY))
  604. break;
  605. sk->sk_state &= ~PPPOX_RELAY;
  606. err = 0;
  607. break;
  608. default:
  609. err = -ENOTTY;
  610. }
  611. return err;
  612. }
  613. static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
  614. struct msghdr *m, size_t total_len)
  615. {
  616. struct sk_buff *skb;
  617. struct sock *sk = sock->sk;
  618. struct pppox_sock *po = pppox_sk(sk);
  619. int error;
  620. struct pppoe_hdr hdr;
  621. struct pppoe_hdr *ph;
  622. struct net_device *dev;
  623. char *start;
  624. lock_sock(sk);
  625. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  626. error = -ENOTCONN;
  627. goto end;
  628. }
  629. hdr.ver = 1;
  630. hdr.type = 1;
  631. hdr.code = 0;
  632. hdr.sid = po->num;
  633. dev = po->pppoe_dev;
  634. error = -EMSGSIZE;
  635. if (total_len > (dev->mtu + dev->hard_header_len))
  636. goto end;
  637. skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
  638. 0, GFP_KERNEL);
  639. if (!skb) {
  640. error = -ENOMEM;
  641. goto end;
  642. }
  643. /* Reserve space for headers. */
  644. skb_reserve(skb, dev->hard_header_len);
  645. skb_reset_network_header(skb);
  646. skb->dev = dev;
  647. skb->priority = sk->sk_priority;
  648. skb->protocol = __constant_htons(ETH_P_PPP_SES);
  649. ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  650. start = (char *) &ph->tag[0];
  651. error = memcpy_fromiovec(start, m->msg_iov, total_len);
  652. if (error < 0) {
  653. kfree_skb(skb);
  654. goto end;
  655. }
  656. error = total_len;
  657. dev->hard_header(skb, dev, ETH_P_PPP_SES,
  658. po->pppoe_pa.remote, NULL, total_len);
  659. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  660. ph->length = htons(total_len);
  661. dev_queue_xmit(skb);
  662. end:
  663. release_sock(sk);
  664. return error;
  665. }
  666. /************************************************************************
  667. *
  668. * xmit function for internal use.
  669. *
  670. ***********************************************************************/
  671. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  672. {
  673. struct pppox_sock *po = pppox_sk(sk);
  674. struct net_device *dev = po->pppoe_dev;
  675. struct pppoe_hdr hdr;
  676. struct pppoe_hdr *ph;
  677. int headroom = skb_headroom(skb);
  678. int data_len = skb->len;
  679. struct sk_buff *skb2;
  680. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  681. goto abort;
  682. hdr.ver = 1;
  683. hdr.type = 1;
  684. hdr.code = 0;
  685. hdr.sid = po->num;
  686. hdr.length = htons(skb->len);
  687. if (!dev)
  688. goto abort;
  689. /* Copy the skb if there is no space for the header. */
  690. if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
  691. skb2 = dev_alloc_skb(32+skb->len +
  692. sizeof(struct pppoe_hdr) +
  693. dev->hard_header_len);
  694. if (skb2 == NULL)
  695. goto abort;
  696. skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
  697. skb_copy_from_linear_data(skb, skb_put(skb2, skb->len),
  698. skb->len);
  699. } else {
  700. /* Make a clone so as to not disturb the original skb,
  701. * give dev_queue_xmit something it can free.
  702. */
  703. skb2 = skb_clone(skb, GFP_ATOMIC);
  704. if (skb2 == NULL)
  705. goto abort;
  706. }
  707. ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
  708. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  709. skb2->protocol = __constant_htons(ETH_P_PPP_SES);
  710. skb_reset_network_header(skb2);
  711. skb2->dev = dev;
  712. dev->hard_header(skb2, dev, ETH_P_PPP_SES,
  713. po->pppoe_pa.remote, NULL, data_len);
  714. /* We're transmitting skb2, and assuming that dev_queue_xmit
  715. * will free it. The generic ppp layer however, is expecting
  716. * that we give back 'skb' (not 'skb2') in case of failure,
  717. * but free it in case of success.
  718. */
  719. if (dev_queue_xmit(skb2) < 0)
  720. goto abort;
  721. kfree_skb(skb);
  722. return 1;
  723. abort:
  724. return 0;
  725. }
  726. /************************************************************************
  727. *
  728. * xmit function called by generic PPP driver
  729. * sends PPP frame over PPPoE socket
  730. *
  731. ***********************************************************************/
  732. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  733. {
  734. struct sock *sk = (struct sock *) chan->private;
  735. return __pppoe_xmit(sk, skb);
  736. }
  737. static struct ppp_channel_ops pppoe_chan_ops = {
  738. .start_xmit = pppoe_xmit,
  739. };
  740. static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
  741. struct msghdr *m, size_t total_len, int flags)
  742. {
  743. struct sock *sk = sock->sk;
  744. struct sk_buff *skb;
  745. int error = 0;
  746. if (sk->sk_state & PPPOX_BOUND) {
  747. error = -EIO;
  748. goto end;
  749. }
  750. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  751. flags & MSG_DONTWAIT, &error);
  752. if (error < 0)
  753. goto end;
  754. m->msg_namelen = 0;
  755. if (skb) {
  756. struct pppoe_hdr *ph = pppoe_hdr(skb);
  757. const int len = ntohs(ph->length);
  758. error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
  759. if (error == 0)
  760. error = len;
  761. }
  762. kfree_skb(skb);
  763. end:
  764. return error;
  765. }
  766. #ifdef CONFIG_PROC_FS
  767. static int pppoe_seq_show(struct seq_file *seq, void *v)
  768. {
  769. struct pppox_sock *po;
  770. char *dev_name;
  771. if (v == SEQ_START_TOKEN) {
  772. seq_puts(seq, "Id Address Device\n");
  773. goto out;
  774. }
  775. po = v;
  776. dev_name = po->pppoe_pa.dev;
  777. seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
  778. po->pppoe_pa.sid,
  779. po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
  780. po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
  781. po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
  782. out:
  783. return 0;
  784. }
  785. static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
  786. {
  787. struct pppox_sock *po;
  788. int i = 0;
  789. for (; i < PPPOE_HASH_SIZE; i++) {
  790. po = item_hash_table[i];
  791. while (po) {
  792. if (!pos--)
  793. goto out;
  794. po = po->next;
  795. }
  796. }
  797. out:
  798. return po;
  799. }
  800. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  801. {
  802. loff_t l = *pos;
  803. read_lock_bh(&pppoe_hash_lock);
  804. return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
  805. }
  806. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  807. {
  808. struct pppox_sock *po;
  809. ++*pos;
  810. if (v == SEQ_START_TOKEN) {
  811. po = pppoe_get_idx(0);
  812. goto out;
  813. }
  814. po = v;
  815. if (po->next)
  816. po = po->next;
  817. else {
  818. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  819. while (++hash < PPPOE_HASH_SIZE) {
  820. po = item_hash_table[hash];
  821. if (po)
  822. break;
  823. }
  824. }
  825. out:
  826. return po;
  827. }
  828. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  829. {
  830. read_unlock_bh(&pppoe_hash_lock);
  831. }
  832. static struct seq_operations pppoe_seq_ops = {
  833. .start = pppoe_seq_start,
  834. .next = pppoe_seq_next,
  835. .stop = pppoe_seq_stop,
  836. .show = pppoe_seq_show,
  837. };
  838. static int pppoe_seq_open(struct inode *inode, struct file *file)
  839. {
  840. return seq_open(file, &pppoe_seq_ops);
  841. }
  842. static const struct file_operations pppoe_seq_fops = {
  843. .owner = THIS_MODULE,
  844. .open = pppoe_seq_open,
  845. .read = seq_read,
  846. .llseek = seq_lseek,
  847. .release = seq_release,
  848. };
  849. static int __init pppoe_proc_init(void)
  850. {
  851. struct proc_dir_entry *p;
  852. p = create_proc_entry("net/pppoe", S_IRUGO, NULL);
  853. if (!p)
  854. return -ENOMEM;
  855. p->proc_fops = &pppoe_seq_fops;
  856. return 0;
  857. }
  858. #else /* CONFIG_PROC_FS */
  859. static inline int pppoe_proc_init(void) { return 0; }
  860. #endif /* CONFIG_PROC_FS */
  861. static const struct proto_ops pppoe_ops = {
  862. .family = AF_PPPOX,
  863. .owner = THIS_MODULE,
  864. .release = pppoe_release,
  865. .bind = sock_no_bind,
  866. .connect = pppoe_connect,
  867. .socketpair = sock_no_socketpair,
  868. .accept = sock_no_accept,
  869. .getname = pppoe_getname,
  870. .poll = datagram_poll,
  871. .listen = sock_no_listen,
  872. .shutdown = sock_no_shutdown,
  873. .setsockopt = sock_no_setsockopt,
  874. .getsockopt = sock_no_getsockopt,
  875. .sendmsg = pppoe_sendmsg,
  876. .recvmsg = pppoe_recvmsg,
  877. .mmap = sock_no_mmap,
  878. .ioctl = pppox_ioctl,
  879. };
  880. static struct pppox_proto pppoe_proto = {
  881. .create = pppoe_create,
  882. .ioctl = pppoe_ioctl,
  883. .owner = THIS_MODULE,
  884. };
  885. static int __init pppoe_init(void)
  886. {
  887. int err = proto_register(&pppoe_sk_proto, 0);
  888. if (err)
  889. goto out;
  890. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  891. if (err)
  892. goto out_unregister_pppoe_proto;
  893. err = pppoe_proc_init();
  894. if (err)
  895. goto out_unregister_pppox_proto;
  896. dev_add_pack(&pppoes_ptype);
  897. dev_add_pack(&pppoed_ptype);
  898. register_netdevice_notifier(&pppoe_notifier);
  899. out:
  900. return err;
  901. out_unregister_pppox_proto:
  902. unregister_pppox_proto(PX_PROTO_OE);
  903. out_unregister_pppoe_proto:
  904. proto_unregister(&pppoe_sk_proto);
  905. goto out;
  906. }
  907. static void __exit pppoe_exit(void)
  908. {
  909. unregister_pppox_proto(PX_PROTO_OE);
  910. dev_remove_pack(&pppoes_ptype);
  911. dev_remove_pack(&pppoed_ptype);
  912. unregister_netdevice_notifier(&pppoe_notifier);
  913. remove_proc_entry("net/pppoe", NULL);
  914. proto_unregister(&pppoe_sk_proto);
  915. }
  916. module_init(pppoe_init);
  917. module_exit(pppoe_exit);
  918. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  919. MODULE_DESCRIPTION("PPP over Ethernet driver");
  920. MODULE_LICENSE("GPL");
  921. MODULE_ALIAS_NETPROTO(PF_PPPOX);