really fix the linux-atm package
[openwrt.git] / package / linux-atm / patches / 100-br2684.patch
1 --- linux-atm-2.4.1.orig/src/br2684/br2684ctl.c
2 +++ linux-atm-2.4.1/src/br2684/br2684ctl.c
3 @@ -0,0 +1,251 @@
4 +#include <stdio.h>
5 +#include <stdlib.h>
6 +#include <unistd.h>
7 +#include <errno.h>
8 +#include <sys/ioctl.h>
9 +#include <string.h>
10 +#include <syslog.h>
11 +#include <atm.h>
12 +#include <linux/atmdev.h>
13 +#include <linux/atmbr2684.h>
14 +
15 +/* Written by Marcell GAL <cell@sch.bme.hu> to make use of the */
16 +/* ioctls defined in the br2684... kernel patch */
17 +/* Compile with cc -o br2684ctl br2684ctl.c -latm */
18 +
19 +/*
20 +  Modified feb 2001 by Stephen Aaskov (saa@lasat.com)
21 +  - Added daemonization code
22 +  - Added syslog
23 +  
24 +  TODO: Delete interfaces after exit?
25 +*/
26 +
27 +
28 +#define LOG_NAME "RFC1483/2684 bridge"
29 +#define LOG_OPTION     LOG_PERROR
30 +#define LOG_FACILITY   LOG_LOCAL0
31 +
32 +
33 +int lastsock, lastitf;
34 +
35 +
36 +void fatal(const char *str, int i)
37 +{
38 +  syslog (LOG_ERR,"Fatal: %s",str);
39 +  exit(-2);
40 +};
41 +
42 +
43 +void exitFunc(void)
44 +{
45 +  syslog (LOG_PID,"Daemon terminated\n");      
46 +}
47 +
48 +
49 +int create_br(char *nstr)
50 +{
51 +  int num, err;
52 +  
53 +  if(lastsock<0) {
54 +    lastsock = socket(PF_ATMPVC, SOCK_DGRAM, ATM_AAL5);
55 +  }
56 +  if (lastsock<0) {
57 +    syslog(LOG_ERR, "socket creation failed: %s",strerror(errno));
58 +  } else {
59 +    /* create the device with ioctl: */
60 +    num=atoi(nstr);
61 +    if( num>=0 && num<1234567890){
62 +      struct atm_newif_br2684 ni;
63 +      ni.backend_num = ATM_BACKEND_BR2684;
64 +      ni.media = BR2684_MEDIA_ETHERNET;
65 +      ni.mtu = 1500;
66 +      sprintf(ni.ifname, "nas%d", num);
67 +      err=ioctl (lastsock, ATM_NEWBACKENDIF, &ni);
68 +  
69 +      if (err == 0)
70 +       syslog(LOG_INFO, "Interface \"%s\" created sucessfully\n",ni.ifname);
71 +      else
72 +       syslog(LOG_INFO, "Interface \"%s\" could not be created, reason: %s\n",
73 +              ni.ifname,
74 +              strerror(errno));
75 +      lastitf=num;     /* even if we didn't create, because existed, assign_vcc wil want to know it! */
76 +    } else {
77 +      syslog(LOG_ERR,"err: strange interface number %d", num );
78 +    }
79 +  }
80 +  return 0;
81 +}
82 +
83 +
84 +int assign_vcc(char *astr, int encap, int bufsize, struct atm_qos qos)
85 +{
86 +    int err;
87 +    struct sockaddr_atmpvc addr;
88 +    int fd;
89 +    struct atm_backend_br2684 be;
90 +
91 +    memset(&addr, 0, sizeof(addr));
92 +    err=text2atm(astr,(struct sockaddr *)(&addr), sizeof(addr), T2A_PVC);
93 +    if (err!=0)
94 +      syslog(LOG_ERR,"Could not parse ATM parameters (error=%d)\n",err);
95 +    
96 +#if 0
97 +    addr.sap_family = AF_ATMPVC;
98 +    addr.sap_addr.itf = itf;
99 +    addr.sap_addr.vpi = 0;
100 +    addr.sap_addr.vci = vci;
101 +#endif
102 +    syslog(LOG_INFO,"Communicating over ATM %d.%d.%d, encapsulation: %s\n", addr.sap_addr.itf,
103 +          addr.sap_addr.vpi,
104 +          addr.sap_addr.vci,
105 +          encap?"VC mux":"LLC");
106 +    
107 +    if ((fd = socket(PF_ATMPVC, SOCK_DGRAM, ATM_AAL5)) < 0)
108 +      syslog(LOG_ERR,"failed to create socket %d, reason: %s", errno,strerror(errno));
109 +    
110 +    if (qos.aal == 0) {
111 +      qos.aal                     = ATM_AAL5;
112 +      qos.txtp.traffic_class      = ATM_UBR;
113 +      qos.txtp.max_sdu            = 1524;
114 +      qos.txtp.pcr                = ATM_MAX_PCR;
115 +      qos.rxtp = qos.txtp;
116 +    }
117 +
118 +    if ( (err=setsockopt(fd,SOL_SOCKET,SO_SNDBUF, &bufsize ,sizeof(bufsize))) )
119 +      syslog(LOG_ERR,"setsockopt SO_SNDBUF: (%d) %s\n",err, strerror(err));
120 +    
121 +    if (setsockopt(fd, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0)
122 +      syslog(LOG_ERR,"setsockopt SO_ATMQOS %d", errno);
123 +
124 +    err = connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_atmpvc));
125 +    
126 +    if (err < 0)
127 +      fatal("failed to connect on socket", err);
128 +    
129 +    /* attach the vcc to device: */
130 +    
131 +    be.backend_num = ATM_BACKEND_BR2684;
132 +    be.ifspec.method = BR2684_FIND_BYIFNAME;
133 +    sprintf(be.ifspec.spec.ifname, "nas%d", lastitf);
134 +    be.fcs_in = BR2684_FCSIN_NO;
135 +    be.fcs_out = BR2684_FCSOUT_NO;
136 +    be.fcs_auto = 0;
137 +    be.encaps = encap ? BR2684_ENCAPS_VC : BR2684_ENCAPS_LLC;
138 +    be.has_vpiid = 0;
139 +    be.send_padding = 0;
140 +    be.min_size = 0;
141 +    err=ioctl (fd, ATM_SETBACKEND, &be);
142 +    if (err == 0)
143 +      syslog (LOG_INFO,"Interface configured");
144 +    else {
145 +      syslog (LOG_ERR,"Could not configure interface:%s",strerror(errno));
146 +      exit(2);
147 +    }
148 +    return fd ;
149 +}
150 +
151 +
152 +void usage(char *s)
153 +{
154 +  printf("usage: %s [-b] [[-c number] [-e 0|1] [-s sndbuf] [-q qos] [-a [itf.]vpi.vci]*]*\n", s);
155 +  exit(1);
156 +}
157 +
158 +
159 +
160 +int main (int argc, char **argv)
161 +{
162 +  int c, background=0, encap=0, sndbuf=8192;
163 +  struct atm_qos reqqos;
164 +  lastsock=-1;
165 +  lastitf=0;
166 +  
167 +  /* st qos to 0 */
168 +  memset(&reqqos, 0, sizeof(reqqos));
169 +
170 +  openlog (LOG_NAME,LOG_OPTION,LOG_FACILITY);
171 +  if (argc>1)
172 +    while ((c = getopt(argc, argv,"q:a:bc:e:s:?h")) !=EOF)
173 +      switch (c) {
174 +      case 'q':
175 +       printf ("optarg : %s",optarg);
176 +       if (text2qos(optarg,&reqqos,0)) fprintf(stderr,"QOS parameter invalid\n"); 
177 +       break;
178 +      case 'a':
179 +       assign_vcc(optarg, encap, sndbuf, reqqos);
180 +       break;
181 +      case 'b':
182 +       background=1;
183 +       break;
184 +      case 'c':
185 +       create_br(optarg);
186 +       break;
187 +      case 'e':
188 +       encap=(atoi(optarg));
189 +       if(encap<0){
190 +         syslog (LOG_ERR, "invalid encapsulation: %s:\n",optarg);
191 +         encap=0;
192 +       }
193 +       break;
194 +      case 's':
195 +       sndbuf=(atoi(optarg));
196 +       if(sndbuf<0){
197 +         syslog(LOG_ERR, "Invalid sndbuf: %s, using size of 8192 instead\n",optarg);
198 +         sndbuf=8192;
199 +       }
200 +       break;
201 +      case '?':
202 +      case 'h':
203 +      default:
204 +       usage(argv[0]);
205 +      }
206 +  else
207 +    usage(argv[0]);
208 +
209 +  if (argc != optind) usage(argv[0]);
210 +  
211 +  if(lastsock>=0) close(lastsock);
212 +  
213 +  if (background) {
214 +    pid_t pid;
215 +    
216 +    pid=fork();
217 +    if (pid < 0) {
218 +      fprintf(stderr,"Error detaching\n");
219 +      exit(2);
220 +    } else if (pid) 
221 +      exit(0); // This is the parent
222 +    
223 +    // Become a process group and session group leader
224 +    if (setsid()<0) {
225 +      fprintf (stderr,"Could not set process group\n");
226 +      exit(2);
227 +    }
228 +    
229 +    // Fork again to let process group leader exit
230 +    pid = fork();
231 +    if (pid < 0) {
232 +      fprintf(stderr,"Error detaching during second fork\n");
233 +      exit(2);
234 +    } else if (pid)
235 +      exit(0); // This is the parent
236 +    
237 +    // Now we're ready for buisness
238 +    chdir("/");            // Don't keep directories in use
239 +    close(0); close(1); close(2);  // Close stdin, -out and -error
240 +    /*
241 +      Note that this implementation does not keep an open 
242 +      stdout/err.
243 +      If we need them they can be opened now
244 +    */
245 +    
246 +  }
247 +  
248 +  syslog (LOG_INFO, "RFC 1483/2684 bridge daemon started\n");  
249 +  atexit (exitFunc);
250 +  
251 +  while (1) sleep(30); /* to keep the sockets... */
252 +  return 0;
253 +}
254 +
255 diff -ruN linux-atm-2.4.1/configure.in linux-atm-2.4.1.new/configure.in
256 --- linux-atm-2.4.1/configure.in        2003-04-25 04:17:05.000000000 +0200
257 +++ linux-atm-2.4.1.new/configure.in    2005-07-27 15:45:49.532396543 +0200
258 @@ -153,26 +153,6 @@
259                 src/Makefile \
260                 src/include/Makefile \
261                 src/lib/Makefile \
262 -               src/test/Makefile \
263 -               src/debug/Makefile \
264 -               src/qgen/Makefile \
265 -               src/saal/Makefile \
266 -               src/sigd/Makefile \
267 -               src/maint/Makefile \
268 -               src/arpd/Makefile \
269 -               src/ilmid/Makefile \
270 -               src/ilmid/asn1/Makefile \
271 -               src/man/Makefile \
272 -               src/led/Makefile \
273 -               src/lane/Makefile \
274 -               src/mpoad/Makefile \
275 -               src/switch/Makefile \
276 -               src/switch/debug/Makefile \
277 -               src/switch/tcp/Makefile \
278 -               src/config/Makefile \
279 -               src/config/init-redhat/Makefile \
280 -               src/extra/Makefile \
281 -               src/extra/linux-atm.spec \
282 -               src/extra/ANS/Makefile
283 +               src/br2684/Makefile \
284                 )
285  
286 diff -ruN linux-atm-2.4.1/src/br2684/Makefile linux-atm-2.4.1.new/src/br2684/Makefile
287 --- linux-atm-2.4.1/src/br2684/Makefile 1970-01-01 02:00:00.000000000 +0200
288 +++ linux-atm-2.4.1.new/src/br2684/Makefile     2002-07-15 23:44:25.000000000 +0200
289 @@ -0,0 +1,13 @@
290 +PREFIX=${TI_FILESYSTEM}
291 +
292 +all: br2684ctl
293 +
294 +br2684ctl: br2684ctl.c
295 +       gcc -latm -o br2684ctl br2684ctl.c 
296 +       strip br2684ctl
297 +
298 +install: br2684ctl
299 +       cp br2684ctl $(PREFIX)/usr/sbin/
300 +
301 +clean:
302 +       rm -rf br2684ctl *.o
303 diff -ruN linux-atm-2.4.1/src/Makefile.am linux-atm-2.4.1.new/src/Makefile.am
304 --- linux-atm-2.4.1/src/Makefile.am     2001-10-03 23:14:53.000000000 +0200
305 +++ linux-atm-2.4.1.new/src/Makefile.am 2005-07-27 15:33:52.389309711 +0200
306 @@ -1,3 +1,2 @@
307 -SUBDIRS = include lib test debug qgen saal sigd maint arpd ilmid man led lane \
308 -               mpoad switch config extra
309 +SUBDIRS = include lib br2684
310  
311