clip.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* net/atm/clip.c - RFC1577 Classical IP over ATM */
  2. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  3. #include <linux/config.h>
  4. #include <linux/string.h>
  5. #include <linux/errno.h>
  6. #include <linux/kernel.h> /* for UINT_MAX */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/wait.h>
  12. #include <linux/timer.h>
  13. #include <linux/if_arp.h> /* for some manifest constants */
  14. #include <linux/notifier.h>
  15. #include <linux/atm.h>
  16. #include <linux/atmdev.h>
  17. #include <linux/atmclip.h>
  18. #include <linux/atmarp.h>
  19. #include <linux/capability.h>
  20. #include <linux/ip.h> /* for net/route.h */
  21. #include <linux/in.h> /* for struct sockaddr_in */
  22. #include <linux/if.h> /* for IFF_UP */
  23. #include <linux/inetdevice.h>
  24. #include <linux/bitops.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/jhash.h>
  29. #include <net/route.h> /* for struct rtable and routing */
  30. #include <net/icmp.h> /* icmp_send */
  31. #include <asm/param.h> /* for HZ */
  32. #include <asm/byteorder.h> /* for htons etc. */
  33. #include <asm/system.h> /* save/restore_flags */
  34. #include <asm/uaccess.h>
  35. #include <asm/atomic.h>
  36. #include "common.h"
  37. #include "resources.h"
  38. #include "ipcommon.h"
  39. #include <net/atmclip.h>
  40. #if 0
  41. #define DPRINTK(format,args...) printk(format,##args)
  42. #else
  43. #define DPRINTK(format,args...)
  44. #endif
  45. static struct net_device *clip_devs;
  46. static struct atm_vcc *atmarpd;
  47. static struct neigh_table clip_tbl;
  48. static struct timer_list idle_timer;
  49. static int start_timer = 1;
  50. static int to_atmarpd(enum atmarp_ctrl_type type,int itf,unsigned long ip)
  51. {
  52. struct sock *sk;
  53. struct atmarp_ctrl *ctrl;
  54. struct sk_buff *skb;
  55. DPRINTK("to_atmarpd(%d)\n",type);
  56. if (!atmarpd) return -EUNATCH;
  57. skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
  58. if (!skb) return -ENOMEM;
  59. ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
  60. ctrl->type = type;
  61. ctrl->itf_num = itf;
  62. ctrl->ip = ip;
  63. atm_force_charge(atmarpd,skb->truesize);
  64. sk = sk_atm(atmarpd);
  65. skb_queue_tail(&sk->sk_receive_queue, skb);
  66. sk->sk_data_ready(sk, skb->len);
  67. return 0;
  68. }
  69. static void link_vcc(struct clip_vcc *clip_vcc,struct atmarp_entry *entry)
  70. {
  71. DPRINTK("link_vcc %p to entry %p (neigh %p)\n",clip_vcc,entry,
  72. entry->neigh);
  73. clip_vcc->entry = entry;
  74. clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
  75. clip_vcc->next = entry->vccs;
  76. entry->vccs = clip_vcc;
  77. entry->neigh->used = jiffies;
  78. }
  79. static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
  80. {
  81. struct atmarp_entry *entry = clip_vcc->entry;
  82. struct clip_vcc **walk;
  83. if (!entry) {
  84. printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n",clip_vcc);
  85. return;
  86. }
  87. spin_lock_bh(&entry->neigh->dev->xmit_lock); /* block clip_start_xmit() */
  88. entry->neigh->used = jiffies;
  89. for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
  90. if (*walk == clip_vcc) {
  91. int error;
  92. *walk = clip_vcc->next; /* atomic */
  93. clip_vcc->entry = NULL;
  94. if (clip_vcc->xoff)
  95. netif_wake_queue(entry->neigh->dev);
  96. if (entry->vccs)
  97. goto out;
  98. entry->expires = jiffies-1;
  99. /* force resolution or expiration */
  100. error = neigh_update(entry->neigh, NULL, NUD_NONE,
  101. NEIGH_UPDATE_F_ADMIN);
  102. if (error)
  103. printk(KERN_CRIT "unlink_clip_vcc: "
  104. "neigh_update failed with %d\n",error);
  105. goto out;
  106. }
  107. printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
  108. "0x%p)\n",entry,clip_vcc);
  109. out:
  110. spin_unlock_bh(&entry->neigh->dev->xmit_lock);
  111. }
  112. /* The neighbour entry n->lock is held. */
  113. static int neigh_check_cb(struct neighbour *n)
  114. {
  115. struct atmarp_entry *entry = NEIGH2ENTRY(n);
  116. struct clip_vcc *cv;
  117. for (cv = entry->vccs; cv; cv = cv->next) {
  118. unsigned long exp = cv->last_use + cv->idle_timeout;
  119. if (cv->idle_timeout && time_after(jiffies, exp)) {
  120. DPRINTK("releasing vcc %p->%p of entry %p\n",
  121. cv, cv->vcc, entry);
  122. vcc_release_async(cv->vcc, -ETIMEDOUT);
  123. }
  124. }
  125. if (entry->vccs || time_before(jiffies, entry->expires))
  126. return 0;
  127. if (atomic_read(&n->refcnt) > 1) {
  128. struct sk_buff *skb;
  129. DPRINTK("destruction postponed with ref %d\n",
  130. atomic_read(&n->refcnt));
  131. while ((skb = skb_dequeue(&n->arp_queue)) != NULL)
  132. dev_kfree_skb(skb);
  133. return 0;
  134. }
  135. DPRINTK("expired neigh %p\n",n);
  136. return 1;
  137. }
  138. static void idle_timer_check(unsigned long dummy)
  139. {
  140. write_lock(&clip_tbl.lock);
  141. __neigh_for_each_release(&clip_tbl, neigh_check_cb);
  142. mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
  143. write_unlock(&clip_tbl.lock);
  144. }
  145. static int clip_arp_rcv(struct sk_buff *skb)
  146. {
  147. struct atm_vcc *vcc;
  148. DPRINTK("clip_arp_rcv\n");
  149. vcc = ATM_SKB(skb)->vcc;
  150. if (!vcc || !atm_charge(vcc,skb->truesize)) {
  151. dev_kfree_skb_any(skb);
  152. return 0;
  153. }
  154. DPRINTK("pushing to %p\n",vcc);
  155. DPRINTK("using %p\n",CLIP_VCC(vcc)->old_push);
  156. CLIP_VCC(vcc)->old_push(vcc,skb);
  157. return 0;
  158. }
  159. static const unsigned char llc_oui[] = {
  160. 0xaa, /* DSAP: non-ISO */
  161. 0xaa, /* SSAP: non-ISO */
  162. 0x03, /* Ctrl: Unnumbered Information Command PDU */
  163. 0x00, /* OUI: EtherType */
  164. 0x00,
  165. 0x00 };
  166. static void clip_push(struct atm_vcc *vcc,struct sk_buff *skb)
  167. {
  168. struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
  169. DPRINTK("clip push\n");
  170. if (!skb) {
  171. DPRINTK("removing VCC %p\n",clip_vcc);
  172. if (clip_vcc->entry) unlink_clip_vcc(clip_vcc);
  173. clip_vcc->old_push(vcc,NULL); /* pass on the bad news */
  174. kfree(clip_vcc);
  175. return;
  176. }
  177. atm_return(vcc,skb->truesize);
  178. skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
  179. /* clip_vcc->entry == NULL if we don't have an IP address yet */
  180. if (!skb->dev) {
  181. dev_kfree_skb_any(skb);
  182. return;
  183. }
  184. ATM_SKB(skb)->vcc = vcc;
  185. skb->mac.raw = skb->data;
  186. if (!clip_vcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
  187. llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
  188. else {
  189. skb->protocol = ((u16 *) skb->data)[3];
  190. skb_pull(skb,RFC1483LLC_LEN);
  191. if (skb->protocol == htons(ETH_P_ARP)) {
  192. PRIV(skb->dev)->stats.rx_packets++;
  193. PRIV(skb->dev)->stats.rx_bytes += skb->len;
  194. clip_arp_rcv(skb);
  195. return;
  196. }
  197. }
  198. clip_vcc->last_use = jiffies;
  199. PRIV(skb->dev)->stats.rx_packets++;
  200. PRIV(skb->dev)->stats.rx_bytes += skb->len;
  201. memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
  202. netif_rx(skb);
  203. }
  204. /*
  205. * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
  206. * clip_pop is atomic with respect to the critical section in clip_start_xmit.
  207. */
  208. static void clip_pop(struct atm_vcc *vcc,struct sk_buff *skb)
  209. {
  210. struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
  211. struct net_device *dev = skb->dev;
  212. int old;
  213. unsigned long flags;
  214. DPRINTK("clip_pop(vcc %p)\n",vcc);
  215. clip_vcc->old_pop(vcc,skb);
  216. /* skb->dev == NULL in outbound ARP packets */
  217. if (!dev) return;
  218. spin_lock_irqsave(&PRIV(dev)->xoff_lock,flags);
  219. if (atm_may_send(vcc,0)) {
  220. old = xchg(&clip_vcc->xoff,0);
  221. if (old) netif_wake_queue(dev);
  222. }
  223. spin_unlock_irqrestore(&PRIV(dev)->xoff_lock,flags);
  224. }
  225. static void clip_neigh_destroy(struct neighbour *neigh)
  226. {
  227. DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh);
  228. if (NEIGH2ENTRY(neigh)->vccs)
  229. printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
  230. NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef;
  231. }
  232. static void clip_neigh_solicit(struct neighbour *neigh,struct sk_buff *skb)
  233. {
  234. DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh,skb);
  235. to_atmarpd(act_need,PRIV(neigh->dev)->number,NEIGH2ENTRY(neigh)->ip);
  236. }
  237. static void clip_neigh_error(struct neighbour *neigh,struct sk_buff *skb)
  238. {
  239. #ifndef CONFIG_ATM_CLIP_NO_ICMP
  240. icmp_send(skb,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,0);
  241. #endif
  242. kfree_skb(skb);
  243. }
  244. static struct neigh_ops clip_neigh_ops = {
  245. .family = AF_INET,
  246. .destructor = clip_neigh_destroy,
  247. .solicit = clip_neigh_solicit,
  248. .error_report = clip_neigh_error,
  249. .output = dev_queue_xmit,
  250. .connected_output = dev_queue_xmit,
  251. .hh_output = dev_queue_xmit,
  252. .queue_xmit = dev_queue_xmit,
  253. };
  254. static int clip_constructor(struct neighbour *neigh)
  255. {
  256. struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
  257. struct net_device *dev = neigh->dev;
  258. struct in_device *in_dev;
  259. struct neigh_parms *parms;
  260. DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh,entry);
  261. neigh->type = inet_addr_type(entry->ip);
  262. if (neigh->type != RTN_UNICAST) return -EINVAL;
  263. rcu_read_lock();
  264. in_dev = __in_dev_get_rcu(dev);
  265. if (!in_dev) {
  266. rcu_read_unlock();
  267. return -EINVAL;
  268. }
  269. parms = in_dev->arp_parms;
  270. __neigh_parms_put(neigh->parms);
  271. neigh->parms = neigh_parms_clone(parms);
  272. rcu_read_unlock();
  273. neigh->ops = &clip_neigh_ops;
  274. neigh->output = neigh->nud_state & NUD_VALID ?
  275. neigh->ops->connected_output : neigh->ops->output;
  276. entry->neigh = neigh;
  277. entry->vccs = NULL;
  278. entry->expires = jiffies-1;
  279. return 0;
  280. }
  281. static u32 clip_hash(const void *pkey, const struct net_device *dev)
  282. {
  283. return jhash_2words(*(u32 *)pkey, dev->ifindex, clip_tbl.hash_rnd);
  284. }
  285. static struct neigh_table clip_tbl = {
  286. .family = AF_INET,
  287. .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry),
  288. .key_len = 4,
  289. .hash = clip_hash,
  290. .constructor = clip_constructor,
  291. .id = "clip_arp_cache",
  292. /* parameters are copied from ARP ... */
  293. .parms = {
  294. .tbl = &clip_tbl,
  295. .base_reachable_time = 30 * HZ,
  296. .retrans_time = 1 * HZ,
  297. .gc_staletime = 60 * HZ,
  298. .reachable_time = 30 * HZ,
  299. .delay_probe_time = 5 * HZ,
  300. .queue_len = 3,
  301. .ucast_probes = 3,
  302. .mcast_probes = 3,
  303. .anycast_delay = 1 * HZ,
  304. .proxy_delay = (8 * HZ) / 10,
  305. .proxy_qlen = 64,
  306. .locktime = 1 * HZ,
  307. },
  308. .gc_interval = 30 * HZ,
  309. .gc_thresh1 = 128,
  310. .gc_thresh2 = 512,
  311. .gc_thresh3 = 1024,
  312. };
  313. /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
  314. /*
  315. * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
  316. * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
  317. * don't increment the usage count. This is used to create entries in
  318. * clip_setentry.
  319. */
  320. static int clip_encap(struct atm_vcc *vcc,int mode)
  321. {
  322. CLIP_VCC(vcc)->encap = mode;
  323. return 0;
  324. }
  325. static int clip_start_xmit(struct sk_buff *skb,struct net_device *dev)
  326. {
  327. struct clip_priv *clip_priv = PRIV(dev);
  328. struct atmarp_entry *entry;
  329. struct atm_vcc *vcc;
  330. int old;
  331. unsigned long flags;
  332. DPRINTK("clip_start_xmit (skb %p)\n",skb);
  333. if (!skb->dst) {
  334. printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
  335. dev_kfree_skb(skb);
  336. clip_priv->stats.tx_dropped++;
  337. return 0;
  338. }
  339. if (!skb->dst->neighbour) {
  340. #if 0
  341. skb->dst->neighbour = clip_find_neighbour(skb->dst,1);
  342. if (!skb->dst->neighbour) {
  343. dev_kfree_skb(skb); /* lost that one */
  344. clip_priv->stats.tx_dropped++;
  345. return 0;
  346. }
  347. #endif
  348. printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
  349. dev_kfree_skb(skb);
  350. clip_priv->stats.tx_dropped++;
  351. return 0;
  352. }
  353. entry = NEIGH2ENTRY(skb->dst->neighbour);
  354. if (!entry->vccs) {
  355. if (time_after(jiffies, entry->expires)) {
  356. /* should be resolved */
  357. entry->expires = jiffies+ATMARP_RETRY_DELAY*HZ;
  358. to_atmarpd(act_need,PRIV(dev)->number,entry->ip);
  359. }
  360. if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
  361. skb_queue_tail(&entry->neigh->arp_queue,skb);
  362. else {
  363. dev_kfree_skb(skb);
  364. clip_priv->stats.tx_dropped++;
  365. }
  366. return 0;
  367. }
  368. DPRINTK("neigh %p, vccs %p\n",entry,entry->vccs);
  369. ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
  370. DPRINTK("using neighbour %p, vcc %p\n",skb->dst->neighbour,vcc);
  371. if (entry->vccs->encap) {
  372. void *here;
  373. here = skb_push(skb,RFC1483LLC_LEN);
  374. memcpy(here,llc_oui,sizeof(llc_oui));
  375. ((u16 *) here)[3] = skb->protocol;
  376. }
  377. atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
  378. ATM_SKB(skb)->atm_options = vcc->atm_options;
  379. entry->vccs->last_use = jiffies;
  380. DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,vcc,vcc->dev);
  381. old = xchg(&entry->vccs->xoff,1); /* assume XOFF ... */
  382. if (old) {
  383. printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
  384. return 0;
  385. }
  386. clip_priv->stats.tx_packets++;
  387. clip_priv->stats.tx_bytes += skb->len;
  388. (void) vcc->send(vcc,skb);
  389. if (atm_may_send(vcc,0)) {
  390. entry->vccs->xoff = 0;
  391. return 0;
  392. }
  393. spin_lock_irqsave(&clip_priv->xoff_lock,flags);
  394. netif_stop_queue(dev); /* XOFF -> throttle immediately */
  395. barrier();
  396. if (!entry->vccs->xoff)
  397. netif_start_queue(dev);
  398. /* Oh, we just raced with clip_pop. netif_start_queue should be
  399. good enough, because nothing should really be asleep because
  400. of the brief netif_stop_queue. If this isn't true or if it
  401. changes, use netif_wake_queue instead. */
  402. spin_unlock_irqrestore(&clip_priv->xoff_lock,flags);
  403. return 0;
  404. }
  405. static struct net_device_stats *clip_get_stats(struct net_device *dev)
  406. {
  407. return &PRIV(dev)->stats;
  408. }
  409. static int clip_mkip(struct atm_vcc *vcc,int timeout)
  410. {
  411. struct clip_vcc *clip_vcc;
  412. struct sk_buff_head copy;
  413. struct sk_buff *skb;
  414. if (!vcc->push) return -EBADFD;
  415. clip_vcc = kmalloc(sizeof(struct clip_vcc),GFP_KERNEL);
  416. if (!clip_vcc) return -ENOMEM;
  417. DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc,vcc);
  418. clip_vcc->vcc = vcc;
  419. vcc->user_back = clip_vcc;
  420. set_bit(ATM_VF_IS_CLIP, &vcc->flags);
  421. clip_vcc->entry = NULL;
  422. clip_vcc->xoff = 0;
  423. clip_vcc->encap = 1;
  424. clip_vcc->last_use = jiffies;
  425. clip_vcc->idle_timeout = timeout*HZ;
  426. clip_vcc->old_push = vcc->push;
  427. clip_vcc->old_pop = vcc->pop;
  428. vcc->push = clip_push;
  429. vcc->pop = clip_pop;
  430. skb_queue_head_init(&copy);
  431. skb_migrate(&sk_atm(vcc)->sk_receive_queue, &copy);
  432. /* re-process everything received between connection setup and MKIP */
  433. while ((skb = skb_dequeue(&copy)) != NULL)
  434. if (!clip_devs) {
  435. atm_return(vcc,skb->truesize);
  436. kfree_skb(skb);
  437. }
  438. else {
  439. unsigned int len = skb->len;
  440. clip_push(vcc,skb);
  441. PRIV(skb->dev)->stats.rx_packets--;
  442. PRIV(skb->dev)->stats.rx_bytes -= len;
  443. }
  444. return 0;
  445. }
  446. static int clip_setentry(struct atm_vcc *vcc,u32 ip)
  447. {
  448. struct neighbour *neigh;
  449. struct atmarp_entry *entry;
  450. int error;
  451. struct clip_vcc *clip_vcc;
  452. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1 } } };
  453. struct rtable *rt;
  454. if (vcc->push != clip_push) {
  455. printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
  456. return -EBADF;
  457. }
  458. clip_vcc = CLIP_VCC(vcc);
  459. if (!ip) {
  460. if (!clip_vcc->entry) {
  461. printk(KERN_ERR "hiding hidden ATMARP entry\n");
  462. return 0;
  463. }
  464. DPRINTK("setentry: remove\n");
  465. unlink_clip_vcc(clip_vcc);
  466. return 0;
  467. }
  468. error = ip_route_output_key(&rt,&fl);
  469. if (error) return error;
  470. neigh = __neigh_lookup(&clip_tbl,&ip,rt->u.dst.dev,1);
  471. ip_rt_put(rt);
  472. if (!neigh)
  473. return -ENOMEM;
  474. entry = NEIGH2ENTRY(neigh);
  475. if (entry != clip_vcc->entry) {
  476. if (!clip_vcc->entry) DPRINTK("setentry: add\n");
  477. else {
  478. DPRINTK("setentry: update\n");
  479. unlink_clip_vcc(clip_vcc);
  480. }
  481. link_vcc(clip_vcc,entry);
  482. }
  483. error = neigh_update(neigh, llc_oui, NUD_PERMANENT,
  484. NEIGH_UPDATE_F_OVERRIDE|NEIGH_UPDATE_F_ADMIN);
  485. neigh_release(neigh);
  486. return error;
  487. }
  488. static void clip_setup(struct net_device *dev)
  489. {
  490. dev->hard_start_xmit = clip_start_xmit;
  491. /* sg_xmit ... */
  492. dev->get_stats = clip_get_stats;
  493. dev->type = ARPHRD_ATM;
  494. dev->hard_header_len = RFC1483LLC_LEN;
  495. dev->mtu = RFC1626_MTU;
  496. dev->tx_queue_len = 100; /* "normal" queue (packets) */
  497. /* When using a "real" qdisc, the qdisc determines the queue */
  498. /* length. tx_queue_len is only used for the default case, */
  499. /* without any more elaborate queuing. 100 is a reasonable */
  500. /* compromise between decent burst-tolerance and protection */
  501. /* against memory hogs. */
  502. }
  503. static int clip_create(int number)
  504. {
  505. struct net_device *dev;
  506. struct clip_priv *clip_priv;
  507. int error;
  508. if (number != -1) {
  509. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  510. if (PRIV(dev)->number == number) return -EEXIST;
  511. }
  512. else {
  513. number = 0;
  514. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  515. if (PRIV(dev)->number >= number)
  516. number = PRIV(dev)->number+1;
  517. }
  518. dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup);
  519. if (!dev)
  520. return -ENOMEM;
  521. clip_priv = PRIV(dev);
  522. sprintf(dev->name,"atm%d",number);
  523. spin_lock_init(&clip_priv->xoff_lock);
  524. clip_priv->number = number;
  525. error = register_netdev(dev);
  526. if (error) {
  527. free_netdev(dev);
  528. return error;
  529. }
  530. clip_priv->next = clip_devs;
  531. clip_devs = dev;
  532. DPRINTK("registered (net:%s)\n",dev->name);
  533. return number;
  534. }
  535. static int clip_device_event(struct notifier_block *this,unsigned long event,
  536. void *dev)
  537. {
  538. /* ignore non-CLIP devices */
  539. if (((struct net_device *) dev)->type != ARPHRD_ATM ||
  540. ((struct net_device *) dev)->hard_start_xmit != clip_start_xmit)
  541. return NOTIFY_DONE;
  542. switch (event) {
  543. case NETDEV_UP:
  544. DPRINTK("clip_device_event NETDEV_UP\n");
  545. (void) to_atmarpd(act_up,PRIV(dev)->number,0);
  546. break;
  547. case NETDEV_GOING_DOWN:
  548. DPRINTK("clip_device_event NETDEV_DOWN\n");
  549. (void) to_atmarpd(act_down,PRIV(dev)->number,0);
  550. break;
  551. case NETDEV_CHANGE:
  552. case NETDEV_CHANGEMTU:
  553. DPRINTK("clip_device_event NETDEV_CHANGE*\n");
  554. (void) to_atmarpd(act_change,PRIV(dev)->number,0);
  555. break;
  556. case NETDEV_REBOOT:
  557. case NETDEV_REGISTER:
  558. case NETDEV_DOWN:
  559. DPRINTK("clip_device_event %ld\n",event);
  560. /* ignore */
  561. break;
  562. default:
  563. printk(KERN_WARNING "clip_device_event: unknown event "
  564. "%ld\n",event);
  565. break;
  566. }
  567. return NOTIFY_DONE;
  568. }
  569. static int clip_inet_event(struct notifier_block *this,unsigned long event,
  570. void *ifa)
  571. {
  572. struct in_device *in_dev;
  573. in_dev = ((struct in_ifaddr *) ifa)->ifa_dev;
  574. if (!in_dev || !in_dev->dev) {
  575. printk(KERN_WARNING "clip_inet_event: no device\n");
  576. return NOTIFY_DONE;
  577. }
  578. /*
  579. * Transitions are of the down-change-up type, so it's sufficient to
  580. * handle the change on up.
  581. */
  582. if (event != NETDEV_UP) return NOTIFY_DONE;
  583. return clip_device_event(this,NETDEV_CHANGE,in_dev->dev);
  584. }
  585. static struct notifier_block clip_dev_notifier = {
  586. clip_device_event,
  587. NULL,
  588. 0
  589. };
  590. static struct notifier_block clip_inet_notifier = {
  591. clip_inet_event,
  592. NULL,
  593. 0
  594. };
  595. static void atmarpd_close(struct atm_vcc *vcc)
  596. {
  597. DPRINTK("atmarpd_close\n");
  598. atmarpd = NULL; /* assumed to be atomic */
  599. barrier();
  600. unregister_inetaddr_notifier(&clip_inet_notifier);
  601. unregister_netdevice_notifier(&clip_dev_notifier);
  602. if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
  603. printk(KERN_ERR "atmarpd_close: closing with requests "
  604. "pending\n");
  605. skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
  606. DPRINTK("(done)\n");
  607. module_put(THIS_MODULE);
  608. }
  609. static struct atmdev_ops atmarpd_dev_ops = {
  610. .close = atmarpd_close
  611. };
  612. static struct atm_dev atmarpd_dev = {
  613. .ops = &atmarpd_dev_ops,
  614. .type = "arpd",
  615. .number = 999,
  616. .lock = SPIN_LOCK_UNLOCKED
  617. };
  618. static int atm_init_atmarp(struct atm_vcc *vcc)
  619. {
  620. if (atmarpd) return -EADDRINUSE;
  621. if (start_timer) {
  622. start_timer = 0;
  623. init_timer(&idle_timer);
  624. idle_timer.expires = jiffies+CLIP_CHECK_INTERVAL*HZ;
  625. idle_timer.function = idle_timer_check;
  626. add_timer(&idle_timer);
  627. }
  628. atmarpd = vcc;
  629. set_bit(ATM_VF_META,&vcc->flags);
  630. set_bit(ATM_VF_READY,&vcc->flags);
  631. /* allow replies and avoid getting closed if signaling dies */
  632. vcc->dev = &atmarpd_dev;
  633. vcc_insert_socket(sk_atm(vcc));
  634. vcc->push = NULL;
  635. vcc->pop = NULL; /* crash */
  636. vcc->push_oam = NULL; /* crash */
  637. if (register_netdevice_notifier(&clip_dev_notifier))
  638. printk(KERN_ERR "register_netdevice_notifier failed\n");
  639. if (register_inetaddr_notifier(&clip_inet_notifier))
  640. printk(KERN_ERR "register_inetaddr_notifier failed\n");
  641. return 0;
  642. }
  643. static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  644. {
  645. struct atm_vcc *vcc = ATM_SD(sock);
  646. int err = 0;
  647. switch (cmd) {
  648. case SIOCMKCLIP:
  649. case ATMARPD_CTRL:
  650. case ATMARP_MKIP:
  651. case ATMARP_SETENTRY:
  652. case ATMARP_ENCAP:
  653. if (!capable(CAP_NET_ADMIN))
  654. return -EPERM;
  655. break;
  656. default:
  657. return -ENOIOCTLCMD;
  658. }
  659. switch (cmd) {
  660. case SIOCMKCLIP:
  661. err = clip_create(arg);
  662. break;
  663. case ATMARPD_CTRL:
  664. err = atm_init_atmarp(vcc);
  665. if (!err) {
  666. sock->state = SS_CONNECTED;
  667. __module_get(THIS_MODULE);
  668. }
  669. break;
  670. case ATMARP_MKIP:
  671. err = clip_mkip(vcc ,arg);
  672. break;
  673. case ATMARP_SETENTRY:
  674. err = clip_setentry(vcc, arg);
  675. break;
  676. case ATMARP_ENCAP:
  677. err = clip_encap(vcc, arg);
  678. break;
  679. }
  680. return err;
  681. }
  682. static struct atm_ioctl clip_ioctl_ops = {
  683. .owner = THIS_MODULE,
  684. .ioctl = clip_ioctl,
  685. };
  686. #ifdef CONFIG_PROC_FS
  687. static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
  688. {
  689. static int code[] = { 1,2,10,6,1,0 };
  690. static int e164[] = { 1,8,4,6,1,0 };
  691. if (*addr->sas_addr.pub) {
  692. seq_printf(seq, "%s", addr->sas_addr.pub);
  693. if (*addr->sas_addr.prv)
  694. seq_putc(seq, '+');
  695. } else if (!*addr->sas_addr.prv) {
  696. seq_printf(seq, "%s", "(none)");
  697. return;
  698. }
  699. if (*addr->sas_addr.prv) {
  700. unsigned char *prv = addr->sas_addr.prv;
  701. int *fields;
  702. int i, j;
  703. fields = *prv == ATM_AFI_E164 ? e164 : code;
  704. for (i = 0; fields[i]; i++) {
  705. for (j = fields[i]; j; j--)
  706. seq_printf(seq, "%02X", *prv++);
  707. if (fields[i+1])
  708. seq_putc(seq, '.');
  709. }
  710. }
  711. }
  712. /* This means the neighbour entry has no attached VCC objects. */
  713. #define SEQ_NO_VCC_TOKEN ((void *) 2)
  714. static void atmarp_info(struct seq_file *seq, struct net_device *dev,
  715. struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
  716. {
  717. unsigned long exp;
  718. char buf[17];
  719. int svc, llc, off;
  720. svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
  721. (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC));
  722. llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
  723. clip_vcc->encap);
  724. if (clip_vcc == SEQ_NO_VCC_TOKEN)
  725. exp = entry->neigh->used;
  726. else
  727. exp = clip_vcc->last_use;
  728. exp = (jiffies - exp) / HZ;
  729. seq_printf(seq, "%-6s%-4s%-4s%5ld ",
  730. dev->name,
  731. svc ? "SVC" : "PVC",
  732. llc ? "LLC" : "NULL",
  733. exp);
  734. off = scnprintf(buf, sizeof(buf) - 1, "%d.%d.%d.%d",
  735. NIPQUAD(entry->ip));
  736. while (off < 16)
  737. buf[off++] = ' ';
  738. buf[off] = '\0';
  739. seq_printf(seq, "%s", buf);
  740. if (clip_vcc == SEQ_NO_VCC_TOKEN) {
  741. if (time_before(jiffies, entry->expires))
  742. seq_printf(seq, "(resolving)\n");
  743. else
  744. seq_printf(seq, "(expired, ref %d)\n",
  745. atomic_read(&entry->neigh->refcnt));
  746. } else if (!svc) {
  747. seq_printf(seq, "%d.%d.%d\n",
  748. clip_vcc->vcc->dev->number,
  749. clip_vcc->vcc->vpi,
  750. clip_vcc->vcc->vci);
  751. } else {
  752. svc_addr(seq, &clip_vcc->vcc->remote);
  753. seq_putc(seq, '\n');
  754. }
  755. }
  756. struct clip_seq_state {
  757. /* This member must be first. */
  758. struct neigh_seq_state ns;
  759. /* Local to clip specific iteration. */
  760. struct clip_vcc *vcc;
  761. };
  762. static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e,
  763. struct clip_vcc *curr)
  764. {
  765. if (!curr) {
  766. curr = e->vccs;
  767. if (!curr)
  768. return SEQ_NO_VCC_TOKEN;
  769. return curr;
  770. }
  771. if (curr == SEQ_NO_VCC_TOKEN)
  772. return NULL;
  773. curr = curr->next;
  774. return curr;
  775. }
  776. static void *clip_seq_vcc_walk(struct clip_seq_state *state,
  777. struct atmarp_entry *e, loff_t *pos)
  778. {
  779. struct clip_vcc *vcc = state->vcc;
  780. vcc = clip_seq_next_vcc(e, vcc);
  781. if (vcc && pos != NULL) {
  782. while (*pos) {
  783. vcc = clip_seq_next_vcc(e, vcc);
  784. if (!vcc)
  785. break;
  786. --(*pos);
  787. }
  788. }
  789. state->vcc = vcc;
  790. return vcc;
  791. }
  792. static void *clip_seq_sub_iter(struct neigh_seq_state *_state,
  793. struct neighbour *n, loff_t *pos)
  794. {
  795. struct clip_seq_state *state = (struct clip_seq_state *) _state;
  796. return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos);
  797. }
  798. static void *clip_seq_start(struct seq_file *seq, loff_t *pos)
  799. {
  800. return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY);
  801. }
  802. static int clip_seq_show(struct seq_file *seq, void *v)
  803. {
  804. static char atm_arp_banner[] =
  805. "IPitf TypeEncp Idle IP address ATM address\n";
  806. if (v == SEQ_START_TOKEN) {
  807. seq_puts(seq, atm_arp_banner);
  808. } else {
  809. struct clip_seq_state *state = seq->private;
  810. struct neighbour *n = v;
  811. struct clip_vcc *vcc = state->vcc;
  812. atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc);
  813. }
  814. return 0;
  815. }
  816. static struct seq_operations arp_seq_ops = {
  817. .start = clip_seq_start,
  818. .next = neigh_seq_next,
  819. .stop = neigh_seq_stop,
  820. .show = clip_seq_show,
  821. };
  822. static int arp_seq_open(struct inode *inode, struct file *file)
  823. {
  824. struct clip_seq_state *state;
  825. struct seq_file *seq;
  826. int rc = -EAGAIN;
  827. state = kmalloc(sizeof(*state), GFP_KERNEL);
  828. if (!state) {
  829. rc = -ENOMEM;
  830. goto out_kfree;
  831. }
  832. memset(state, 0, sizeof(*state));
  833. state->ns.neigh_sub_iter = clip_seq_sub_iter;
  834. rc = seq_open(file, &arp_seq_ops);
  835. if (rc)
  836. goto out_kfree;
  837. seq = file->private_data;
  838. seq->private = state;
  839. out:
  840. return rc;
  841. out_kfree:
  842. kfree(state);
  843. goto out;
  844. }
  845. static struct file_operations arp_seq_fops = {
  846. .open = arp_seq_open,
  847. .read = seq_read,
  848. .llseek = seq_lseek,
  849. .release = seq_release_private,
  850. .owner = THIS_MODULE
  851. };
  852. #endif
  853. static int __init atm_clip_init(void)
  854. {
  855. neigh_table_init(&clip_tbl);
  856. clip_tbl_hook = &clip_tbl;
  857. register_atm_ioctl(&clip_ioctl_ops);
  858. #ifdef CONFIG_PROC_FS
  859. {
  860. struct proc_dir_entry *p;
  861. p = create_proc_entry("arp", S_IRUGO, atm_proc_root);
  862. if (p)
  863. p->proc_fops = &arp_seq_fops;
  864. }
  865. #endif
  866. return 0;
  867. }
  868. static void __exit atm_clip_exit(void)
  869. {
  870. struct net_device *dev, *next;
  871. remove_proc_entry("arp", atm_proc_root);
  872. deregister_atm_ioctl(&clip_ioctl_ops);
  873. /* First, stop the idle timer, so it stops banging
  874. * on the table.
  875. */
  876. if (start_timer == 0)
  877. del_timer(&idle_timer);
  878. /* Next, purge the table, so that the device
  879. * unregister loop below does not hang due to
  880. * device references remaining in the table.
  881. */
  882. neigh_ifdown(&clip_tbl, NULL);
  883. dev = clip_devs;
  884. while (dev) {
  885. next = PRIV(dev)->next;
  886. unregister_netdev(dev);
  887. free_netdev(dev);
  888. dev = next;
  889. }
  890. /* Now it is safe to fully shutdown whole table. */
  891. neigh_table_clear(&clip_tbl);
  892. clip_tbl_hook = NULL;
  893. }
  894. module_init(atm_clip_init);
  895. module_exit(atm_clip_exit);
  896. MODULE_LICENSE("GPL");