pppoe.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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.mtu = dev->mtu - sizeof(struct pppoe_hdr);
  477. po->chan.private = sk;
  478. po->chan.ops = &pppoe_chan_ops;
  479. error = ppp_register_channel(&po->chan);
  480. if (error)
  481. goto err_put;
  482. sk->sk_state = PPPOX_CONNECTED;
  483. }
  484. po->num = sp->sa_addr.pppoe.sid;
  485. end:
  486. release_sock(sk);
  487. return error;
  488. err_put:
  489. if (po->pppoe_dev) {
  490. dev_put(po->pppoe_dev);
  491. po->pppoe_dev = NULL;
  492. }
  493. goto end;
  494. }
  495. static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
  496. int *usockaddr_len, int peer)
  497. {
  498. int len = sizeof(struct sockaddr_pppox);
  499. struct sockaddr_pppox sp;
  500. sp.sa_family = AF_PPPOX;
  501. sp.sa_protocol = PX_PROTO_OE;
  502. memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
  503. sizeof(struct pppoe_addr));
  504. memcpy(uaddr, &sp, len);
  505. *usockaddr_len = len;
  506. return 0;
  507. }
  508. static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
  509. unsigned long arg)
  510. {
  511. struct sock *sk = sock->sk;
  512. struct pppox_sock *po = pppox_sk(sk);
  513. int val = 0;
  514. int err = 0;
  515. switch (cmd) {
  516. case PPPIOCGMRU:
  517. err = -ENXIO;
  518. if (!(sk->sk_state & PPPOX_CONNECTED))
  519. break;
  520. err = -EFAULT;
  521. if (put_user(po->pppoe_dev->mtu -
  522. sizeof(struct pppoe_hdr) -
  523. PPP_HDRLEN,
  524. (int __user *) arg))
  525. break;
  526. err = 0;
  527. break;
  528. case PPPIOCSMRU:
  529. err = -ENXIO;
  530. if (!(sk->sk_state & PPPOX_CONNECTED))
  531. break;
  532. err = -EFAULT;
  533. if (get_user(val,(int __user *) arg))
  534. break;
  535. if (val < (po->pppoe_dev->mtu
  536. - sizeof(struct pppoe_hdr)
  537. - PPP_HDRLEN))
  538. err = 0;
  539. else
  540. err = -EINVAL;
  541. break;
  542. case PPPIOCSFLAGS:
  543. err = -EFAULT;
  544. if (get_user(val, (int __user *) arg))
  545. break;
  546. err = 0;
  547. break;
  548. case PPPOEIOCSFWD:
  549. {
  550. struct pppox_sock *relay_po;
  551. err = -EBUSY;
  552. if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
  553. break;
  554. err = -ENOTCONN;
  555. if (!(sk->sk_state & PPPOX_CONNECTED))
  556. break;
  557. /* PPPoE address from the user specifies an outbound
  558. PPPoE address to which frames are forwarded to */
  559. err = -EFAULT;
  560. if (copy_from_user(&po->pppoe_relay,
  561. (void __user *)arg,
  562. sizeof(struct sockaddr_pppox)))
  563. break;
  564. err = -EINVAL;
  565. if (po->pppoe_relay.sa_family != AF_PPPOX ||
  566. po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
  567. break;
  568. /* Check that the socket referenced by the address
  569. actually exists. */
  570. relay_po = get_item_by_addr(&po->pppoe_relay);
  571. if (!relay_po)
  572. break;
  573. sock_put(sk_pppox(relay_po));
  574. sk->sk_state |= PPPOX_RELAY;
  575. err = 0;
  576. break;
  577. }
  578. case PPPOEIOCDFWD:
  579. err = -EALREADY;
  580. if (!(sk->sk_state & PPPOX_RELAY))
  581. break;
  582. sk->sk_state &= ~PPPOX_RELAY;
  583. err = 0;
  584. break;
  585. default:;
  586. };
  587. return err;
  588. }
  589. static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
  590. struct msghdr *m, size_t total_len)
  591. {
  592. struct sk_buff *skb = NULL;
  593. struct sock *sk = sock->sk;
  594. struct pppox_sock *po = pppox_sk(sk);
  595. int error = 0;
  596. struct pppoe_hdr hdr;
  597. struct pppoe_hdr *ph;
  598. struct net_device *dev;
  599. char *start;
  600. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
  601. error = -ENOTCONN;
  602. goto end;
  603. }
  604. hdr.ver = 1;
  605. hdr.type = 1;
  606. hdr.code = 0;
  607. hdr.sid = po->num;
  608. lock_sock(sk);
  609. dev = po->pppoe_dev;
  610. error = -EMSGSIZE;
  611. if (total_len > (dev->mtu + dev->hard_header_len))
  612. goto end;
  613. skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
  614. 0, GFP_KERNEL);
  615. if (!skb) {
  616. error = -ENOMEM;
  617. goto end;
  618. }
  619. /* Reserve space for headers. */
  620. skb_reserve(skb, dev->hard_header_len);
  621. skb->nh.raw = skb->data;
  622. skb->dev = dev;
  623. skb->priority = sk->sk_priority;
  624. skb->protocol = __constant_htons(ETH_P_PPP_SES);
  625. ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
  626. start = (char *) &ph->tag[0];
  627. error = memcpy_fromiovec(start, m->msg_iov, total_len);
  628. if (error < 0) {
  629. kfree_skb(skb);
  630. goto end;
  631. }
  632. error = total_len;
  633. dev->hard_header(skb, dev, ETH_P_PPP_SES,
  634. po->pppoe_pa.remote, NULL, total_len);
  635. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  636. ph->length = htons(total_len);
  637. dev_queue_xmit(skb);
  638. end:
  639. release_sock(sk);
  640. return error;
  641. }
  642. /************************************************************************
  643. *
  644. * xmit function for internal use.
  645. *
  646. ***********************************************************************/
  647. static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
  648. {
  649. struct pppox_sock *po = pppox_sk(sk);
  650. struct net_device *dev = po->pppoe_dev;
  651. struct pppoe_hdr hdr;
  652. struct pppoe_hdr *ph;
  653. int headroom = skb_headroom(skb);
  654. int data_len = skb->len;
  655. struct sk_buff *skb2;
  656. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  657. goto abort;
  658. hdr.ver = 1;
  659. hdr.type = 1;
  660. hdr.code = 0;
  661. hdr.sid = po->num;
  662. hdr.length = htons(skb->len);
  663. if (!dev)
  664. goto abort;
  665. /* Copy the skb if there is no space for the header. */
  666. if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
  667. skb2 = dev_alloc_skb(32+skb->len +
  668. sizeof(struct pppoe_hdr) +
  669. dev->hard_header_len);
  670. if (skb2 == NULL)
  671. goto abort;
  672. skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
  673. memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
  674. } else {
  675. /* Make a clone so as to not disturb the original skb,
  676. * give dev_queue_xmit something it can free.
  677. */
  678. skb2 = skb_clone(skb, GFP_ATOMIC);
  679. if (skb2 == NULL)
  680. goto abort;
  681. }
  682. ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
  683. memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
  684. skb2->protocol = __constant_htons(ETH_P_PPP_SES);
  685. skb2->nh.raw = skb2->data;
  686. skb2->dev = dev;
  687. dev->hard_header(skb2, dev, ETH_P_PPP_SES,
  688. po->pppoe_pa.remote, NULL, data_len);
  689. /* We're transmitting skb2, and assuming that dev_queue_xmit
  690. * will free it. The generic ppp layer however, is expecting
  691. * that we give back 'skb' (not 'skb2') in case of failure,
  692. * but free it in case of success.
  693. */
  694. if (dev_queue_xmit(skb2) < 0)
  695. goto abort;
  696. kfree_skb(skb);
  697. return 1;
  698. abort:
  699. return 0;
  700. }
  701. /************************************************************************
  702. *
  703. * xmit function called by generic PPP driver
  704. * sends PPP frame over PPPoE socket
  705. *
  706. ***********************************************************************/
  707. static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  708. {
  709. struct sock *sk = (struct sock *) chan->private;
  710. return __pppoe_xmit(sk, skb);
  711. }
  712. static struct ppp_channel_ops pppoe_chan_ops = {
  713. .start_xmit = pppoe_xmit,
  714. };
  715. static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
  716. struct msghdr *m, size_t total_len, int flags)
  717. {
  718. struct sock *sk = sock->sk;
  719. struct sk_buff *skb = NULL;
  720. int error = 0;
  721. int len;
  722. struct pppoe_hdr *ph = NULL;
  723. if (sk->sk_state & PPPOX_BOUND) {
  724. error = -EIO;
  725. goto end;
  726. }
  727. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  728. flags & MSG_DONTWAIT, &error);
  729. if (error < 0) {
  730. goto end;
  731. }
  732. m->msg_namelen = 0;
  733. if (skb) {
  734. error = 0;
  735. ph = (struct pppoe_hdr *) skb->nh.raw;
  736. len = ntohs(ph->length);
  737. error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
  738. if (error < 0)
  739. goto do_skb_free;
  740. error = len;
  741. }
  742. do_skb_free:
  743. if (skb)
  744. kfree_skb(skb);
  745. end:
  746. return error;
  747. }
  748. #ifdef CONFIG_PROC_FS
  749. static int pppoe_seq_show(struct seq_file *seq, void *v)
  750. {
  751. struct pppox_sock *po;
  752. char *dev_name;
  753. if (v == SEQ_START_TOKEN) {
  754. seq_puts(seq, "Id Address Device\n");
  755. goto out;
  756. }
  757. po = v;
  758. dev_name = po->pppoe_pa.dev;
  759. seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
  760. po->pppoe_pa.sid,
  761. po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
  762. po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
  763. po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
  764. out:
  765. return 0;
  766. }
  767. static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
  768. {
  769. struct pppox_sock *po = NULL;
  770. int i = 0;
  771. for (; i < PPPOE_HASH_SIZE; i++) {
  772. po = item_hash_table[i];
  773. while (po) {
  774. if (!pos--)
  775. goto out;
  776. po = po->next;
  777. }
  778. }
  779. out:
  780. return po;
  781. }
  782. static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
  783. {
  784. loff_t l = *pos;
  785. read_lock_bh(&pppoe_hash_lock);
  786. return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
  787. }
  788. static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  789. {
  790. struct pppox_sock *po;
  791. ++*pos;
  792. if (v == SEQ_START_TOKEN) {
  793. po = pppoe_get_idx(0);
  794. goto out;
  795. }
  796. po = v;
  797. if (po->next)
  798. po = po->next;
  799. else {
  800. int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
  801. while (++hash < PPPOE_HASH_SIZE) {
  802. po = item_hash_table[hash];
  803. if (po)
  804. break;
  805. }
  806. }
  807. out:
  808. return po;
  809. }
  810. static void pppoe_seq_stop(struct seq_file *seq, void *v)
  811. {
  812. read_unlock_bh(&pppoe_hash_lock);
  813. }
  814. static struct seq_operations pppoe_seq_ops = {
  815. .start = pppoe_seq_start,
  816. .next = pppoe_seq_next,
  817. .stop = pppoe_seq_stop,
  818. .show = pppoe_seq_show,
  819. };
  820. static int pppoe_seq_open(struct inode *inode, struct file *file)
  821. {
  822. return seq_open(file, &pppoe_seq_ops);
  823. }
  824. static struct file_operations pppoe_seq_fops = {
  825. .owner = THIS_MODULE,
  826. .open = pppoe_seq_open,
  827. .read = seq_read,
  828. .llseek = seq_lseek,
  829. .release = seq_release,
  830. };
  831. static int __init pppoe_proc_init(void)
  832. {
  833. struct proc_dir_entry *p;
  834. p = create_proc_entry("net/pppoe", S_IRUGO, NULL);
  835. if (!p)
  836. return -ENOMEM;
  837. p->proc_fops = &pppoe_seq_fops;
  838. return 0;
  839. }
  840. #else /* CONFIG_PROC_FS */
  841. static inline int pppoe_proc_init(void) { return 0; }
  842. #endif /* CONFIG_PROC_FS */
  843. static const struct proto_ops pppoe_ops = {
  844. .family = AF_PPPOX,
  845. .owner = THIS_MODULE,
  846. .release = pppoe_release,
  847. .bind = sock_no_bind,
  848. .connect = pppoe_connect,
  849. .socketpair = sock_no_socketpair,
  850. .accept = sock_no_accept,
  851. .getname = pppoe_getname,
  852. .poll = datagram_poll,
  853. .listen = sock_no_listen,
  854. .shutdown = sock_no_shutdown,
  855. .setsockopt = sock_no_setsockopt,
  856. .getsockopt = sock_no_getsockopt,
  857. .sendmsg = pppoe_sendmsg,
  858. .recvmsg = pppoe_recvmsg,
  859. .mmap = sock_no_mmap,
  860. .ioctl = pppox_ioctl,
  861. };
  862. static struct pppox_proto pppoe_proto = {
  863. .create = pppoe_create,
  864. .ioctl = pppoe_ioctl,
  865. .owner = THIS_MODULE,
  866. };
  867. static int __init pppoe_init(void)
  868. {
  869. int err = proto_register(&pppoe_sk_proto, 0);
  870. if (err)
  871. goto out;
  872. err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
  873. if (err)
  874. goto out_unregister_pppoe_proto;
  875. err = pppoe_proc_init();
  876. if (err)
  877. goto out_unregister_pppox_proto;
  878. dev_add_pack(&pppoes_ptype);
  879. dev_add_pack(&pppoed_ptype);
  880. register_netdevice_notifier(&pppoe_notifier);
  881. out:
  882. return err;
  883. out_unregister_pppox_proto:
  884. unregister_pppox_proto(PX_PROTO_OE);
  885. out_unregister_pppoe_proto:
  886. proto_unregister(&pppoe_sk_proto);
  887. goto out;
  888. }
  889. static void __exit pppoe_exit(void)
  890. {
  891. unregister_pppox_proto(PX_PROTO_OE);
  892. dev_remove_pack(&pppoes_ptype);
  893. dev_remove_pack(&pppoed_ptype);
  894. unregister_netdevice_notifier(&pppoe_notifier);
  895. remove_proc_entry("net/pppoe", NULL);
  896. proto_unregister(&pppoe_sk_proto);
  897. }
  898. module_init(pppoe_init);
  899. module_exit(pppoe_exit);
  900. MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
  901. MODULE_DESCRIPTION("PPP over Ethernet driver");
  902. MODULE_LICENSE("GPL");
  903. MODULE_ALIAS_NETPROTO(PF_PPPOX);