pppoe.c 26 KB

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