add support for routed RFC2684 encaps. on ATM (patches written by joseangel), close #58
[openwrt.git] / package / linux-atm / patches / 100-br2684.patch
1 diff -ruN linux-atm-2.4.1/configure.in linux-atm-2.4.1.new/configure.in
2 --- linux-atm-2.4.1/configure.in        2003-04-25 04:17:05.000000000 +0200
3 +++ linux-atm-2.4.1.new/configure.in    2005-07-27 15:45:49.532396543 +0200
4 @@ -153,26 +153,6 @@
5                 src/Makefile \
6                 src/include/Makefile \
7                 src/lib/Makefile \
8 -               src/test/Makefile \
9 -               src/debug/Makefile \
10 -               src/qgen/Makefile \
11 -               src/saal/Makefile \
12 -               src/sigd/Makefile \
13 -               src/maint/Makefile \
14 -               src/arpd/Makefile \
15 -               src/ilmid/Makefile \
16 -               src/ilmid/asn1/Makefile \
17 -               src/man/Makefile \
18 -               src/led/Makefile \
19 -               src/lane/Makefile \
20 -               src/mpoad/Makefile \
21 -               src/switch/Makefile \
22 -               src/switch/debug/Makefile \
23 -               src/switch/tcp/Makefile \
24 -               src/config/Makefile \
25 -               src/config/init-redhat/Makefile \
26 -               src/extra/Makefile \
27 -               src/extra/linux-atm.spec \
28 -               src/extra/ANS/Makefile
29 +               src/br2684/Makefile \
30                 )
31  
32 diff -ruN linux-atm-2.4.1/src/br2684/Makefile linux-atm-2.4.1.new/src/br2684/Makefile
33 --- linux-atm-2.4.1/src/br2684/Makefile 1970-01-01 02:00:00.000000000 +0200
34 +++ linux-atm-2.4.1.new/src/br2684/Makefile     2002-07-15 23:44:25.000000000 +0200
35 @@ -0,0 +1,13 @@
36 +PREFIX=${TI_FILESYSTEM}
37 +
38 +all: br2684ctl
39 +
40 +br2684ctl: br2684ctl.c
41 +       gcc -latm -o br2684ctl br2684ctl.c 
42 +       strip br2684ctl
43 +
44 +install: br2684ctl
45 +       cp br2684ctl $(PREFIX)/usr/sbin/
46 +
47 +clean:
48 +       rm -rf br2684ctl *.o
49 diff -ruN linux-atm-2.4.1/src/Makefile.am linux-atm-2.4.1.new/src/Makefile.am
50 --- linux-atm-2.4.1/src/Makefile.am     2001-10-03 23:14:53.000000000 +0200
51 +++ linux-atm-2.4.1.new/src/Makefile.am 2005-07-27 15:33:52.389309711 +0200
52 @@ -1,3 +1,2 @@
53 -SUBDIRS = include lib test debug qgen saal sigd maint arpd ilmid man led lane \
54 -               mpoad switch config extra
55 +SUBDIRS = include lib br2684
56  
57
58 diff -Nur linux-atm-2.4.1.orig/src/include/linux/atmbr2684.h linux-atm-2.4.1/src/include/linux/atmbr2684.h
59 --- linux-atm-2.4.1.orig/src/include/linux/atmbr2684.h  1970-01-01 01:00:00.000000000 +0100
60 +++ linux-atm-2.4.1/src/include/linux/atmbr2684.h       2005-11-13 00:06:42.000000000 +0100
61 @@ -0,0 +1,117 @@
62 +#ifndef _LINUX_ATMBR2684_H
63 +#define _LINUX_ATMBR2684_H
64 +
65 +#include <linux/atm.h>
66 +#include <linux/if.h>          /* For IFNAMSIZ */
67 +#include <linux/if_ether.h>    /* ETH_P_* */
68 +
69 +/*
70 + * Type of media we're bridging (ethernet, token ring, etc)  Currently only
71 + * ethernet is supported
72 + */
73 +#define BR2684_MEDIA_ETHERNET  (0)     /* 802.3 */
74 +#define BR2684_MEDIA_802_4     (1)     /* 802.4 */
75 +#define BR2684_MEDIA_TR                (2)     /* 802.5 - token ring */
76 +#define BR2684_MEDIA_FDDI      (3)
77 +#define BR2684_MEDIA_802_6     (4)     /* 802.6 */
78 +
79 +/*
80 + * Is there FCS inbound on this VC?  This currently isn't supported.
81 + */
82 +#define BR2684_FCSIN_NO                (0)
83 +#define BR2684_FCSIN_IGNORE    (1)
84 +#define BR2684_FCSIN_VERIFY    (2)
85 +
86 +/*
87 + * Is there FCS outbound on this VC?  This currently isn't supported.
88 + */
89 +#define BR2684_FCSOUT_NO       (0)
90 +#define BR2684_FCSOUT_SENDZERO (1)
91 +#define BR2684_FCSOUT_GENERATE (2)
92 +
93 +/*
94 + * Does this VC include LLC encapsulation?
95 + */
96 +#define BR2684_ENCAPS_VC       (0)     /* VC-mux */
97 +#define BR2684_ENCAPS_LLC      (1)
98 +#define BR2684_ENCAPS_AUTODETECT (2)   /* Unsuported */
99 +
100 +/*
101 + * Is this VC bridged or routed?
102 + */
103 +
104 +#define        BR2684_PAYLOAD_ROUTED   (0)
105 +#define        BR2684_PAYLOAD_BRIDGED  (1)
106 +
107 +
108 +/*
109 + * This is for the ATM_NEWBACKENDIF call - these are like socket families:
110 + * the first element of the structure is the backend number and the rest
111 + * is per-backend specific
112 + */
113 +struct atm_newif_br2684 {
114 +       atm_backend_t backend_num;      /* ATM_BACKEND_BR2684 */
115 +       int media;              /* BR2684_MEDIA_* */
116 +       char ifname[IFNAMSIZ];
117 +       int mtu;
118 +       int payload;            /* bridged or routed */
119 +};
120 +
121 +/*
122 + * This structure is used to specify a br2684 interface - either by a
123 + * positive integer (returned by ATM_NEWBACKENDIF) or the interfaces name
124 + */
125 +#define BR2684_FIND_BYNOTHING  (0)
126 +#define BR2684_FIND_BYNUM      (1)
127 +#define BR2684_FIND_BYIFNAME   (2)
128 +struct br2684_if_spec {
129 +       int method;             /* BR2684_FIND_* */
130 +       union {
131 +               char ifname[IFNAMSIZ];
132 +               int devnum;
133 +       } spec;
134 +};
135 +
136 +/*
137 + * This is for the ATM_SETBACKEND call - these are like socket families:
138 + * the first element of the structure is the backend number and the rest
139 + * is per-backend specific
140 + */
141 +struct atm_backend_br2684 {
142 +       atm_backend_t backend_num;      /* ATM_BACKEND_BR2684 */
143 +       struct br2684_if_spec ifspec;
144 +       int fcs_in;             /* BR2684_FCSIN_* */
145 +       int fcs_out;            /* BR2684_FCSOUT_* */
146 +       int fcs_auto;           /* 1: fcs_{in,out} disabled if no FCS rx'ed */
147 +       int encaps;             /* BR2684_ENCAPS_* */
148 +       int payload;            /* BR2684_PAYLOAD_* */
149 +       int has_vpiid;          /* 1: use vpn_id - Unsupported */
150 +       __u8 vpn_id[7];
151 +       int send_padding;       /* unsupported */
152 +       int min_size;           /* we will pad smaller packets than this */
153 +};
154 +
155 +/*
156 + * The BR2684_SETFILT ioctl is an experimental mechanism for folks
157 + * terminating a large number of IP-only vcc's.  When netfilter allows
158 + * efficient per-if in/out filters, this support will be removed
159 + */
160 +struct br2684_filter {
161 +       __u32 prefix;           /* network byte order */
162 +       __u32 netmask;          /* 0 = disable filter */
163 +};
164 +
165 +struct br2684_filter_set {
166 +       struct br2684_if_spec ifspec;
167 +       struct br2684_filter filter;
168 +};
169 +
170 +enum br2684_payload {
171 +       p_routed = BR2684_PAYLOAD_ROUTED,
172 +       p_bridged = BR2684_PAYLOAD_BRIDGED,
173 +};
174 +
175 +#define BR2684_SETFILT _IOW( 'a', ATMIOC_BACKEND + 0, \
176 +                               struct br2684_filter_set)
177 +
178 +#endif                         /* _LINUX_ATMBR2684_H */
179 diff Nur linux-atm-2.4.1.orig/src/br2684/br2684ctl.c linux-atm-2.4.1/src/br2684/br2684ctl.c
180 --- linux-atm-2.4.1.orig/src/br2684/br2684ctl.c
181 +++ linux-atm-2.4.1/src/br2684/br2684ctl.c
182 @@ -0,0 +1,302 @@
183 +#include <stdio.h>
184 +#include <stdlib.h>
185 +#include <unistd.h>
186 +#include <errno.h>
187 +#include <sys/ioctl.h>
188 +#include <string.h>
189 +#include <syslog.h>
190 +#include <atm.h>
191 +#include <linux/atmdev.h>
192 +#include <linux/atmbr2684.h>
193 +
194 +/* Written by Marcell GAL <cell@sch.bme.hu> to make use of the */
195 +/* ioctls defined in the br2684... kernel patch */
196 +/* Compile with cc -o br2684ctl br2684ctl.c -latm */
197 +
198 +/*
199 +  Modified feb 2001 by Stephen Aaskov (saa@lasat.com)
200 +  - Added daemonization code
201 +  - Added syslog
202 +  
203 +  TODO: Delete interfaces after exit?
204 +*/
205 +
206 +
207 +#define LOG_NAME "RFC1483/2684 bridge"
208 +#define LOG_OPTION     LOG_PERROR
209 +#define LOG_FACILITY   LOG_LOCAL0
210 +
211 +
212 +int lastsock, lastitf;
213 +
214 +void fatal(char *str, int i)
215 +{
216 +       syslog(LOG_ERR, "Fatal: %s", str);
217 +       exit(-2);
218 +};
219 +
220 +
221 +void exitFunc(void)
222 +{
223 +       syslog(LOG_PID, "Daemon terminated\n");
224 +}
225 +
226 +
227 +int create_pidfile(char *nstr)
228 +{
229 +       FILE *pidfile = NULL;
230 +       char name[20];
231 +       int num;
232 +
233 +       if (nstr == NULL)
234 +               return -1;
235 +       num = atoi(nstr);
236 +       if (num < 0)
237 +               return -1;
238 +
239 +       snprintf(name, 20, "/var/run/nas%d.pid", num);
240 +       pidfile = fopen(name, "w");
241 +       if (pidfile == NULL)
242 +               return -1;
243 +       fprintf(pidfile, "%d", getpid());
244 +       fclose(pidfile);
245 +
246 +       return 0;
247 +}
248 +
249 +int create_br(char *nstr, int payload)
250 +{
251 +       int num, err;
252 +
253 +       if (lastsock < 0) {
254 +               lastsock = socket(PF_ATMPVC, SOCK_DGRAM, ATM_AAL5);
255 +       }
256 +       if (lastsock < 0) {
257 +               syslog(LOG_ERR, "socket creation failed: %s",
258 +                      strerror(errno));
259 +       } else {
260 +               /* create the device with ioctl: */
261 +               num = atoi(nstr);
262 +               if (num >= 0 && num < 1234567890) {
263 +                       struct atm_newif_br2684 ni;
264 +                       ni.backend_num = ATM_BACKEND_BR2684;
265 +                       ni.media = BR2684_MEDIA_ETHERNET;
266 +                       ni.mtu = 1500;
267 +                       ni.payload = payload;   /* bridged or routed */
268 +                       sprintf(ni.ifname, "nas%d", num);
269 +                       err = ioctl(lastsock, ATM_NEWBACKENDIF, &ni);
270 +
271 +                       if (err == 0)
272 +                               syslog(LOG_INFO,
273 +                                      "Interface \"%s\" created sucessfully\n",
274 +                                      ni.ifname);
275 +                       else
276 +                               syslog(LOG_INFO,
277 +                                      "Interface \"%s\" could not be created, reason: %s\n",
278 +                                      ni.ifname, strerror(errno));
279 +                       lastitf = num;  /* even if we didn't create, because existed, assign_vcc wil want to know it! */
280 +               } else {
281 +                       syslog(LOG_ERR, "err: strange interface number %d",
282 +                              num);
283 +               }
284 +       }
285 +       return 0;
286 +}
287 +
288 +
289 +int assign_vcc(char *astr, int encap, int payload, int bufsize)
290 +{
291 +       int err, errno;
292 +       struct atm_qos qos;
293 +       struct sockaddr_atmpvc addr;
294 +       int fd;
295 +       struct atm_backend_br2684 be;
296 +
297 +       memset(&addr, 0, sizeof(addr));
298 +       err =
299 +           text2atm(astr, (struct sockaddr *) (&addr), sizeof(addr),
300 +                    T2A_PVC);
301 +       if (err != 0)
302 +               syslog(LOG_ERR,
303 +                      "Could not parse ATM parameters (error=%d)\n", err);
304 +
305 +#if 0
306 +       addr.sap_family = AF_ATMPVC;
307 +       addr.sap_addr.itf = itf;
308 +       addr.sap_addr.vpi = 0;
309 +       addr.sap_addr.vci = vci;
310 +#endif
311 +       syslog(LOG_INFO,
312 +              "Communicating over ATM %d.%d.%d, encapsulation: %s\n",
313 +              addr.sap_addr.itf, addr.sap_addr.vpi, addr.sap_addr.vci,
314 +              encap ? "VC mux" : "LLC");
315 +
316 +       if ((fd = socket(PF_ATMPVC, SOCK_DGRAM, ATM_AAL5)) < 0)
317 +               syslog(LOG_ERR, "failed to create socket %d, reason: %s",
318 +                      errno, strerror(errno));
319 +
320 +
321 +       memset(&qos, 0, sizeof(qos));
322 +       qos.aal = ATM_AAL5;
323 +       qos.txtp.traffic_class = ATM_UBR;
324 +       qos.txtp.max_sdu = 1524;
325 +       qos.txtp.pcr = ATM_MAX_PCR;
326 +       qos.rxtp = qos.txtp;
327 +
328 +       if ((err =
329 +            setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bufsize,
330 +                       sizeof(bufsize))))
331 +               syslog(LOG_ERR, "setsockopt SO_SNDBUF: (%d) %s\n", err,
332 +                      strerror(err));
333 +
334 +       if (setsockopt(fd, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0)
335 +               syslog(LOG_ERR, "setsockopt SO_ATMQOS %d", errno);
336 +
337 +       err =
338 +           connect(fd, (struct sockaddr *) &addr,
339 +                   sizeof(struct sockaddr_atmpvc));
340 +
341 +       if (err < 0)
342 +               fatal("failed to connect on socket", err);
343 +
344 +       /* attach the vcc to device: */
345 +
346 +       be.backend_num = ATM_BACKEND_BR2684;
347 +       be.ifspec.method = BR2684_FIND_BYIFNAME;
348 +       sprintf(be.ifspec.spec.ifname, "nas%d", lastitf);
349 +       be.fcs_in = BR2684_FCSIN_NO;
350 +       be.fcs_out = BR2684_FCSOUT_NO;
351 +       be.fcs_auto = 0;
352 +       be.encaps = encap ? BR2684_ENCAPS_VC : BR2684_ENCAPS_LLC;
353 +       be.payload = payload;
354 +       be.has_vpiid = 0;
355 +       be.send_padding = 0;
356 +       be.min_size = 0;
357 +       err = ioctl(fd, ATM_SETBACKEND, &be);
358 +       if (err == 0)
359 +               syslog(LOG_INFO, "Interface configured");
360 +       else {
361 +               syslog(LOG_ERR, "Could not configure interface:%s",
362 +                      strerror(errno));
363 +               exit(2);
364 +       }
365 +       return fd;
366 +}
367 +
368 +
369 +
370 +void usage(char *s)
371 +{
372 +       printf
373 +           ("usage: %s [-b] [[-c number] [-e 0|1] [-p 0|1] [-t 4|6] [-a [itf.]vpi.vci]*]*\n",
374 +            s);
375 +       exit(1);
376 +}
377 +
378 +
379 +
380 +int main(int argc, char **argv)
381 +{
382 +       int c, background = 0, encap = 0, sndbuf = 8192, payload = 1;
383 +       char *itfnum = NULL;
384 +
385 +       lastsock = -1;
386 +       lastitf = 0;
387 +
388 +       openlog(LOG_NAME, LOG_OPTION, LOG_FACILITY);
389 +       if (argc > 1)
390 +               while ((c = getopt(argc, argv, "a:bc:e:s:p:t:?h")) != EOF)
391 +                       switch (c) {
392 +                       case 'a':
393 +                               assign_vcc(optarg, encap, payload, sndbuf);
394 +                               break;
395 +                       case 'b':
396 +                               background = 1;
397 +                               break;
398 +                       case 'c':
399 +                               create_br(optarg, payload);
400 +                               itfnum = strdup(optarg);
401 +                               break;
402 +                       case 'e':
403 +                               encap = (atoi(optarg));
404 +                               if (encap < 0) {
405 +                                       syslog(LOG_ERR,
406 +                                              "invalid encapsulation: %s:\n",
407 +                                              optarg);
408 +                                       encap = 0;
409 +                               }
410 +                               break;
411 +                       case 's':
412 +                               sndbuf = (atoi(optarg));
413 +                               if (sndbuf < 0) {
414 +                                       syslog(LOG_ERR,
415 +                                              "Invalid sndbuf: %s, using size of 8192 instead\n",
416 +                                              optarg);
417 +                                       sndbuf = 8192;
418 +                               }
419 +                               break;
420 +                       case 'p':       /* payload type: routed (0) or bridged (1) */
421 +                               payload = atoi(optarg);
422 +                               break;
423 +                       case '?':
424 +                       case 'h':
425 +                       default:
426 +                               usage(argv[0]);
427 +       } else
428 +               usage(argv[0]);
429 +
430 +       if (argc != optind)
431 +               usage(argv[0]);
432 +
433 +       if (lastsock >= 0)
434 +               close(lastsock);
435 +
436 +       if (background) {
437 +               pid_t pid;
438 +
439 +               pid = fork();
440 +               if (pid < 0) {
441 +                       fprintf(stderr, "Error detaching\n");
442 +                       exit(2);
443 +               } else if (pid)
444 +                       exit(0);        // This is the parent
445 +
446 +               // Become a process group and session group leader
447 +               if (setsid() < 0) {
448 +                       fprintf(stderr, "Could not set process group\n");
449 +                       exit(2);
450 +               }
451 +               // Fork again to let process group leader exit
452 +               pid = fork();
453 +               if (pid < 0) {
454 +                       fprintf(stderr,
455 +                               "Error detaching during second fork\n");
456 +                       exit(2);
457 +               } else if (pid)
458 +                       exit(0);        // This is the parent
459 +
460 +               // Now we're ready for buisness
461 +               chdir("/");     // Don't keep directories in use
462 +               close(0);
463 +               close(1);
464 +               close(2);       // Close stdin, -out and -error
465 +               /*
466 +                  Note that this implementation does not keep an open 
467 +                  stdout/err.
468 +                  If we need them they can be opened now
469 +                */
470 +
471 +       }
472 +
473 +       if (itfnum != NULL) {
474 +               create_pidfile(itfnum);
475 +               free(itfnum);
476 +       }
477 +
478 +       syslog(LOG_INFO, "RFC 1483/2684 bridge daemon started\n");
479 +       atexit(exitFunc);
480 +
481 +       while (1)
482 +               sleep(30);      /* to keep the sockets... */
483 +       return 0;
484 +}