pppoe.c 25 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. * 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 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(skb, sizeof(struct pppoe_hdr));
  276. skb_postpull_rcsum(skb, ph, sizeof(*ph));
  277. if (pskb_trim_rcsum(skb, len))
  278. goto abort_kfree;
  279. ppp_input(&po->chan, skb);
  280. } else if (sk->sk_state & PPPOX_RELAY) {
  281. relay_po = get_item_by_addr(&po->pppoe_relay);
  282. if (relay_po == NULL)
  283. goto abort_kfree;
  284. if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
  285. goto abort_put;
  286. skb_pull(skb, sizeof(struct pppoe_hdr));
  287. if (!__pppoe_xmit(sk_pppox(relay_po), skb))
  288. goto abort_put;
  289. } else {
  290. if (sock_queue_rcv_skb(sk, skb))
  291. goto abort_kfree;
  292. }
  293. return NET_RX_SUCCESS;
  294. abort_put:
  295. sock_put(sk_pppox(relay_po));
  296. abort_kfree:
  297. kfree_skb(skb);
  298. return NET_RX_DROP;
  299. }
  300. /************************************************************************
  301. *
  302. * Receive wrapper called in BH context.
  303. *
  304. ***********************************************************************/
  305. static int pppoe_rcv(struct sk_buff *skb,
  306. struct net_device *dev,
  307. struct packet_type *pt)
  308. {
  309. struct pppoe_hdr *ph;
  310. struct pppox_sock *po;
  311. struct sock *sk;
  312. int ret;
  313. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  314. goto drop;
  315. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  316. goto out;
  317. ph = (struct pppoe_hdr *) skb->nh.raw;
  318. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
  319. if (!po)
  320. goto drop;
  321. sk = sk_pppox(po);
  322. bh_lock_sock(sk);
  323. /* Socket state is unknown, must put skb into backlog. */
  324. if (sock_owned_by_user(sk) != 0) {
  325. sk_add_backlog(sk, skb);
  326. ret = NET_RX_SUCCESS;
  327. } else {
  328. ret = pppoe_rcv_core(sk, skb);
  329. }
  330. bh_unlock_sock(sk);
  331. sock_put(sk);
  332. return ret;
  333. drop:
  334. kfree_skb(skb);
  335. out:
  336. return NET_RX_DROP;
  337. }
  338. /************************************************************************
  339. *
  340. * Receive a PPPoE Discovery frame.
  341. * This is solely for detection of PADT frames
  342. *
  343. ***********************************************************************/
  344. static int pppoe_disc_rcv(struct sk_buff *skb,
  345. struct net_device *dev,
  346. struct packet_type *pt)
  347. {
  348. struct pppoe_hdr *ph;
  349. struct pppox_sock *po;
  350. if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
  351. goto abort;
  352. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  353. goto out;
  354. ph = (struct pppoe_hdr *) skb->nh.raw;
  355. if (ph->code != PADT_CODE)
  356. goto abort;
  357. po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
  358. if (po) {
  359. struct sock *sk = sk_pppox(po);
  360. bh_lock_sock(sk);
  361. /* If the user has locked the socket, just ignore
  362. * the packet. With the way two rcv protocols hook into
  363. * one socket family type, we cannot (easily) distinguish
  364. * what kind of SKB it is during backlog rcv.
  365. */
  366. if (sock_owned_by_user(sk) == 0) {
  367. /* We're no longer connect at the PPPOE layer,
  368. * and must wait for ppp channel to disconnect us.
  369. */
  370. sk->sk_state = PPPOX_ZOMBIE;
  371. }
  372. bh_unlock_sock(sk);
  373. sock_put(sk);
  374. }
  375. abort:
  376. kfree_skb(skb);
  377. out:
  378. return NET_RX_SUCCESS; /* Lies... :-) */
  379. }
  380. static struct packet_type pppoes_ptype = {
  381. .type = __constant_htons(ETH_P_PPP_SES),
  382. .func = pppoe_rcv,
  383. };
  384. static struct packet_type pppoed_ptype = {
  385. .type = __constant_htons(ETH_P_PPP_DISC),
  386. .func = pppoe_disc_rcv,
  387. };
  388. static struct proto pppoe_sk_proto = {
  389. .name = "PPPOE",
  390. .owner = THIS_MODULE,
  391. .obj_size = sizeof(struct pppox_sock),
  392. };
  393. /***********************************************************************
  394. *
  395. * Initialize a new struct sock.
  396. *
  397. **********************************************************************/
  398. static int pppoe_create(struct socket *sock)
  399. {
  400. int error = -ENOMEM;
  401. struct sock *sk;
  402. sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
  403. if (!sk)
  404. goto out;
  405. sock_init_data(sock, sk);
  406. sock->state = SS_UNCONNECTED;
  407. sock->ops = &pppoe_ops;
  408. sk->sk_backlog_rcv = pppoe_rcv_core;
  409. sk->sk_state = PPPOX_NONE;
  410. sk->sk_type = SOCK_STREAM;
  411. sk->sk_family = PF_PPPOX;
  412. sk->sk_protocol = PX_PROTO_OE;
  413. error = 0;
  414. out: return error;
  415. }
  416. static int pppoe_release(struct socket *sock)
  417. {
  418. struct sock *sk = sock->sk;
  419. struct pppox_sock *po;
  420. int error = 0;
  421. if (!sk)
  422. return 0;
  423. if (sock_flag(sk, SOCK_DEAD))
  424. return -EBADF;
  425. pppox_unbind_sock(sk);
  426. /* Signal the death of the socket. */
  427. sk->sk_state = PPPOX_DEAD;
  428. po = pppox_sk(sk);
  429. if (po->pppoe_pa.sid) {
  430. delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  431. }
  432. if (po->pppoe_dev)
  433. dev_put(po->pppoe_dev);
  434. po->pppoe_dev = NULL;
  435. sock_orphan(sk);
  436. sock->sk = NULL;
  437. skb_queue_purge(&sk->sk_receive_queue);
  438. sock_put(sk);
  439. return error;
  440. }
  441. static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
  442. int sockaddr_len, int flags)
  443. {
  444. struct sock *sk = sock->sk;
  445. struct net_device *dev = NULL;
  446. struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
  447. struct pppox_sock *po = pppox_sk(sk);
  448. int error;
  449. lock_sock(sk);
  450. error = -EINVAL;
  451. if (sp->sa_protocol != PX_PROTO_OE)
  452. goto end;
  453. /* Check for already bound sockets */
  454. error = -EBUSY;
  455. if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
  456. goto end;
  457. /* Check for already disconnected sockets, on attempts to disconnect */
  458. error = -EALREADY;
  459. if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
  460. goto end;
  461. error = 0;
  462. if (po->pppoe_pa.sid) {
  463. pppox_unbind_sock(sk);
  464. /* Delete the old binding */
  465. delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
  466. if(po->pppoe_dev)
  467. dev_put(po->pppoe_dev);
  468. memset(sk_pppox(po) + 1, 0,
  469. sizeof(struct pppox_sock) - sizeof(struct sock));
  470. sk->sk_state = PPPOX_NONE;
  471. }
  472. /* Don't re-bind if sid==0 */
  473. if (sp->sa_addr.pppoe.sid != 0) {
  474. dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
  475. error = -ENODEV;
  476. if (!dev)
  477. goto end;
  478. po->pppoe_dev = dev;
  479. if (!(dev->flags & IFF_UP))
  480. goto err_put;
  481. memcpy(&po->pppoe_pa,
  482. &sp->sa_addr.pppoe,
  483. sizeof(struct pppoe_addr));
  484. error = set_item(po);
  485. if (error < 0)
  486. goto err_put;
  487. po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
  488. dev->hard_header_len);
  489. po->chan.private = sk;
  490. po->chan.ops = &pppoe_chan_ops;
  491. error = ppp_register_channel(&po->chan);
  492. if (error)
  493. goto err_put;
  494. sk->sk_state = PPPOX_CONNECTED;
  495. }
  496. po->num = sp->sa_addr.pppoe.sid;
  497. end:
  498. release_sock(sk);
  499. return error;
  500. err_put:
  501. if (po->pppoe_dev) {
  502. dev_put(po->pppoe_dev);
  503. po->pppoe_dev = NULL;
  504. }
  505. goto end;
  506. }
  507. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  508. int *usockaddr_len, int peer)
  509. {
  510. int len = sizeof(struct sockaddr_pppox);
  511. struct sockaddr_pppox sp;
  512. sp.sa_family = AF_PPPOX;
  513. sp.sa_protocol = PX_PROTO_OE;
  514. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  515. sizeof(struct pppoe_addr));
  516. memcpy(uaddr, &sp, len);
  517. *usockaddr_len = len;
  518. return 0;
  519. }
  520. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  521. unsigned long arg)
  522. {
  523. struct sock *sk = sock->sk;
  524. struct pppox_sock *po = pppox_sk(sk);
  525. int val = 0;
  526. int err = 0;
  527. switch (cmd) {
  528. case PPPIOCGMRU:
  529. err = -ENXIO;
  530. if (!(sk->sk_state & PPPOX_CONNECTED))
  531. break;
  532. err = -EFAULT;
  533. if (put_user(po->pppoe_dev->mtu -
  534. sizeof(struct pppoe_hdr) -
  535. PPP_HDRLEN,
  536. (int __user *) arg))
  537. break;
  538. err = 0;
  539. break;
  540. case PPPIOCSMRU:
  541. err = -ENXIO;
  542. if (!(sk->sk_state & PPPOX_CONNECTED))
  543. break;
  544. err = -EFAULT;
  545. if (get_user(val,(int __user *) arg))
  546. break;
  547. if (val < (po->pppoe_dev->mtu
  548. - sizeof(struct pppoe_hdr)
  549. - PPP_HDRLEN))
  550. err = 0;
  551. else
  552. err = -EINVAL;
  553. break;
  554. case PPPIOCSFLAGS:
  555. err = -EFAULT;
  556. if (get_user(val, (int __user *) arg))
  557. break;
  558. err = 0;
  559. break;
  560. case PPPOEIOCSFWD:
  561. {
  562. struct pppox_sock *relay_po;
  563. err = -EBUSY;
  564. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  565. break;
  566. err = -ENOTCONN;
  567. if (!(sk->sk_state & PPPOX_CONNECTED))
  568. break;
  569. /* PPPoE address from the user specifies an outbound
  570. PPPoE address to which frames are forwarded to */
  571. err = -EFAULT;
  572. if (copy_from_user(&po->pppoe_relay,
  573. (void __user *)arg,
  574. sizeof(struct sockaddr_pppox)))
  575. break;
  576. err = -EINVAL;
  577. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  578. po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
  579. break;
  580. /* Check that the socket referenced by the address
  581. actually exists. */
  582. relay_po = get_item_by_addr(&po->pppoe_relay);
  583. if (!relay_po)
  584. break;
  585. sock_put(sk_pppox(relay_po));
  586. sk->sk_state |= PPPOX_RELAY;
  587. err = 0;
  588. break;
  589. }
  590. case PPPOEIOCDFWD:
  591. err = -EALREADY;
  592. if (!(sk->sk_state & PPPOX_RELAY))
  593. break;
  594. sk->sk_state &= ~PPPOX_RELAY;
  595. err = 0;
  596. break;
  597. default:;
  598. };
  599. return err;
  600. }
  601. static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
  602. struct msghdr *m, size_t total_len)
  603. {
  604. struct sk_buff *skb = NULL;
  605. struct sock *sk = sock->sk;
  606. struct pppox_sock *po = pppox_sk(sk);
  607. int error = 0;
  608. struct pppoe_hdr hdr;
  609. struct pppoe_hdr *ph;
  610. struct net_device *dev;
  611. char *start;
  612. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  613. error = -ENOTCONN;
  614. goto end;
  615. }
  616. hdr.ver = 1;
  617. hdr.type = 1;
  618. hdr.code = 0;
  619. hdr.sid = po->num;
  620. lock_sock(sk);
  621. dev = po->pppoe_dev;
  622. error = -EMSGSIZE;
  623. if (total_len > (dev->mtu + dev->hard_header_len))
  624. goto end;
  625. skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
  626. 0, GFP_KERNEL);
  627. if (!skb) {
  628. error = -ENOMEM;
  629. goto end;
  630. }
  631. /* Reserve space for headers. */
  632. skb_reserve(skb, dev->hard_header_len);
  633. skb->nh.raw = skb->data;
  634. skb->dev = dev;
  635. skb->priority = sk->sk_priority;
  636. skb->protocol = __constant_htons(ETH_P_PPP_SES);
  637. ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  638. start = (char *) &ph->tag[0];
  639. error = memcpy_fromiovec(start, m->msg_iov, total_len);
  640. if (error < 0) {
  641. kfree_skb(skb);
  642. goto end;
  643. }
  644. error = total_len;
  645. dev->hard_header(skb, dev, ETH_P_PPP_SES,
  646. po->pppoe_pa.remote, NULL, total_len);
  647. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  648. ph->length = htons(total_len);
  649. dev_queue_xmit(skb);
  650. end:
  651. release_sock(sk);
  652. return error;
  653. }
  654. /************************************************************************
  655. *
  656. * xmit function for internal use.
  657. *
  658. ***********************************************************************/
  659. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  660. {
  661. struct pppox_sock *po = pppox_sk(sk);
  662. struct net_device *dev = po->pppoe_dev;
  663. struct pppoe_hdr hdr;
  664. struct pppoe_hdr *ph;
  665. int headroom = skb_headroom(skb);
  666. int data_len = skb->len;
  667. struct sk_buff *skb2;
  668. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  669. goto abort;
  670. hdr.ver = 1;
  671. hdr.type = 1;
  672. hdr.code = 0;
  673. hdr.sid = po->num;
  674. hdr.length = htons(skb->len);
  675. if (!dev)
  676. goto abort;
  677. /* Copy the skb if there is no space for the header. */
  678. if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
  679. skb2 = dev_alloc_skb(32+skb->len +
  680. sizeof(struct pppoe_hdr) +
  681. dev->hard_header_len);
  682. if (skb2 == NULL)
  683. goto abort;
  684. skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
  685. memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
  686. } else {
  687. /* Make a clone so as to not disturb the original skb,
  688. * give dev_queue_xmit something it can free.
  689. */
  690. skb2 = skb_clone(skb, GFP_ATOMIC);
  691. }
  692. ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
  693. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  694. skb2->protocol = __constant_htons(ETH_P_PPP_SES);
  695. skb2->nh.raw = skb2->data;
  696. skb2->dev = dev;
  697. dev->hard_header(skb2, dev, ETH_P_PPP_SES,
  698. po->pppoe_pa.remote, NULL, data_len);
  699. /* We're transmitting skb2, and assuming that dev_queue_xmit
  700. * will free it. The generic ppp layer however, is expecting
  701. * that we give back 'skb' (not 'skb2') in case of failure,
  702. * but free it in case of success.
  703. */
  704. if (dev_queue_xmit(skb2) < 0)
  705. goto abort;
  706. kfree_skb(skb);
  707. return 1;
  708. abort:
  709. return 0;
  710. }
  711. /************************************************************************
  712. *
  713. * xmit function called by generic PPP driver
  714. * sends PPP frame over PPPoE socket
  715. *
  716. ***********************************************************************/
  717. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  718. {
  719. struct sock *sk = (struct sock *) chan->private;
  720. return __pppoe_xmit(sk, skb);
  721. }
  722. static struct ppp_channel_ops pppoe_chan_ops = {
  723. .start_xmit = pppoe_xmit,
  724. };
  725. static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
  726. struct msghdr *m, size_t total_len, int flags)
  727. {
  728. struct sock *sk = sock->sk;
  729. struct sk_buff *skb = NULL;
  730. int error = 0;
  731. int len;
  732. struct pppoe_hdr *ph = NULL;
  733. if (sk->sk_state & PPPOX_BOUND) {
  734. error = -EIO;
  735. goto end;
  736. }
  737. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  738. flags & MSG_DONTWAIT, &error);
  739. if (error < 0) {
  740. goto end;
  741. }
  742. m->msg_namelen = 0;
  743. if (skb) {
  744. error = 0;
  745. ph = (struct pppoe_hdr *) skb->nh.raw;
  746. len = ntohs(ph->length);
  747. error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
  748. if (error < 0)
  749. goto do_skb_free;
  750. error = len;
  751. }
  752. do_skb_free:
  753. if (skb)
  754. kfree_skb(skb);
  755. end:
  756. return error;
  757. }
  758. #ifdef CONFIG_PROC_FS
  759. static int pppoe_seq_show(struct seq_file *seq, void *v)
  760. {
  761. struct pppox_sock *po;
  762. char *dev_name;
  763. if (v == SEQ_START_TOKEN) {
  764. seq_puts(seq, "Id Address Device\n");
  765. goto out;
  766. }
  767. po = v;
  768. dev_name = po->pppoe_pa.dev;
  769. seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
  770. po->pppoe_pa.sid,
  771. po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
  772. po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
  773. po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
  774. out:
  775. return 0;
  776. }
  777. static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
  778. {
  779. struct pppox_sock *po = NULL;
  780. int i = 0;
  781. for (; i < PPPOE_HASH_SIZE; i++) {
  782. po = item_hash_table[i];
  783. while (po) {
  784. if (!pos--)
  785. goto out;
  786. po = po->next;
  787. }
  788. }
  789. out:
  790. return po;
  791. }
  792. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  793. {
  794. loff_t l = *pos;
  795. read_lock_bh(&pppoe_hash_lock);
  796. return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
  797. }
  798. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  799. {
  800. struct pppox_sock *po;
  801. ++*pos;
  802. if (v == SEQ_START_TOKEN) {
  803. po = pppoe_get_idx(0);
  804. goto out;
  805. }
  806. po = v;
  807. if (po->next)
  808. po = po->next;
  809. else {
  810. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  811. while (++hash < PPPOE_HASH_SIZE) {
  812. po = item_hash_table[hash];
  813. if (po)
  814. break;
  815. }
  816. }
  817. out:
  818. return po;
  819. }
  820. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  821. {
  822. read_unlock_bh(&pppoe_hash_lock);
  823. }
  824. static struct seq_operations pppoe_seq_ops = {
  825. .start = pppoe_seq_start,
  826. .next = pppoe_seq_next,
  827. .stop = pppoe_seq_stop,
  828. .show = pppoe_seq_show,
  829. };
  830. static int pppoe_seq_open(struct inode *inode, struct file *file)
  831. {
  832. return seq_open(file, &pppoe_seq_ops);
  833. }
  834. static struct file_operations pppoe_seq_fops = {
  835. .owner = THIS_MODULE,
  836. .open = pppoe_seq_open,
  837. .read = seq_read,
  838. .llseek = seq_lseek,
  839. .release = seq_release,
  840. };
  841. static int __init pppoe_proc_init(void)
  842. {
  843. struct proc_dir_entry *p;
  844. p = create_proc_entry("pppoe", S_IRUGO, proc_net);
  845. if (!p)
  846. return -ENOMEM;
  847. p->proc_fops = &pppoe_seq_fops;
  848. return 0;
  849. }
  850. #else /* CONFIG_PROC_FS */
  851. static inline int pppoe_proc_init(void) { return 0; }
  852. #endif /* CONFIG_PROC_FS */
  853. /* ->ioctl are set at pppox_create */
  854. static struct proto_ops pppoe_ops = {
  855. .family = AF_PPPOX,
  856. .owner = THIS_MODULE,
  857. .release = pppoe_release,
  858. .bind = sock_no_bind,
  859. .connect = pppoe_connect,
  860. .socketpair = sock_no_socketpair,
  861. .accept = sock_no_accept,
  862. .getname = pppoe_getname,
  863. .poll = datagram_poll,
  864. .listen = sock_no_listen,
  865. .shutdown = sock_no_shutdown,
  866. .setsockopt = sock_no_setsockopt,
  867. .getsockopt = sock_no_getsockopt,
  868. .sendmsg = pppoe_sendmsg,
  869. .recvmsg = pppoe_recvmsg,
  870. .mmap = sock_no_mmap
  871. };
  872. static struct pppox_proto pppoe_proto = {
  873. .create = pppoe_create,
  874. .ioctl = pppoe_ioctl,
  875. .owner = THIS_MODULE,
  876. };
  877. static int __init pppoe_init(void)
  878. {
  879. int err = proto_register(&pppoe_sk_proto, 0);
  880. if (err)
  881. goto out;
  882. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  883. if (err)
  884. goto out_unregister_pppoe_proto;
  885. err = pppoe_proc_init();
  886. if (err)
  887. goto out_unregister_pppox_proto;
  888. dev_add_pack(&pppoes_ptype);
  889. dev_add_pack(&pppoed_ptype);
  890. register_netdevice_notifier(&pppoe_notifier);
  891. out:
  892. return err;
  893. out_unregister_pppox_proto:
  894. unregister_pppox_proto(PX_PROTO_OE);
  895. out_unregister_pppoe_proto:
  896. proto_unregister(&pppoe_sk_proto);
  897. goto out;
  898. }
  899. static void __exit pppoe_exit(void)
  900. {
  901. unregister_pppox_proto(PX_PROTO_OE);
  902. dev_remove_pack(&pppoes_ptype);
  903. dev_remove_pack(&pppoed_ptype);
  904. unregister_netdevice_notifier(&pppoe_notifier);
  905. remove_proc_entry("pppoe", proc_net);
  906. proto_unregister(&pppoe_sk_proto);
  907. }
  908. module_init(pppoe_init);
  909. module_exit(pppoe_exit);
  910. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  911. MODULE_DESCRIPTION("PPP over Ethernet driver");
  912. MODULE_LICENSE("GPL");
  913. MODULE_ALIAS_NETPROTO(PF_PPPOX);