pppoe.c 25 KB

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