]>
git.ipfire.org Git - people/stevee/ipfire-2.x.git/blob - src/br2684ctl/br2684ctl.c
9 #include <linux/atmdev.h>
10 #include <linux/atmbr2684.h>
12 /* Written by Marcell GAL <cell@sch.bme.hu> to make use of the */
13 /* ioctls defined in the br2684... kernel patch */
14 /* Compile with cc -o br2684ctl br2684ctl.c -latm */
17 Modified feb 2001 by Stephen Aaskov (saa@lasat.com)
18 - Added daemonization code
21 TODO: Delete interfaces after exit?
25 #define LOG_NAME "RFC1483/2684 bridge"
26 #define LOG_OPTION LOG_PERROR
27 #define LOG_FACILITY LOG_LOCAL0
30 int lastsock
, lastitf
;
33 void fatal(const char *str
, int i
)
35 syslog (LOG_ERR
,"Fatal: %s",str
);
42 syslog (LOG_PID
,"Daemon terminated\n");
46 int create_pidfile(int num
)
51 if (num
< 0) return -1;
53 snprintf(name
, 20, "/var/run/nas%d.pid", num
);
54 pidfile
= fopen(name
, "w");
55 if (pidfile
== NULL
) return -1;
56 fprintf(pidfile
, "%d", getpid());
62 int create_br(char *nstr
)
67 lastsock
= socket(PF_ATMPVC
, SOCK_DGRAM
, ATM_AAL5
);
70 syslog(LOG_ERR
, "socket creation failed: %s",strerror(errno
));
72 /* create the device with ioctl: */
74 if( num
>=0 && num
<1234567890){
75 struct atm_newif_br2684 ni
;
76 ni
.backend_num
= ATM_BACKEND_BR2684
;
77 ni
.media
= BR2684_MEDIA_ETHERNET
;
79 sprintf(ni
.ifname
, "nas%d", num
);
80 err
=ioctl (lastsock
, ATM_NEWBACKENDIF
, &ni
);
83 syslog(LOG_INFO
, "Interface \"%s\" created sucessfully\n",ni
.ifname
);
85 syslog(LOG_INFO
, "Interface \"%s\" could not be created, reason: %s\n",
88 lastitf
=num
; /* even if we didn't create, because existed, assign_vcc wil want to know it! */
90 syslog(LOG_ERR
,"err: strange interface number %d", num
);
97 int assign_vcc(char *astr
, int encap
, int bufsize
, struct atm_qos qos
)
100 struct sockaddr_atmpvc addr
;
102 struct atm_backend_br2684 be
;
104 memset(&addr
, 0, sizeof(addr
));
105 err
=text2atm(astr
,(struct sockaddr
*)(&addr
), sizeof(addr
), T2A_PVC
);
107 syslog(LOG_ERR
,"Could not parse ATM parameters (error=%d)\n",err
);
110 addr
.sap_family
= AF_ATMPVC
;
111 addr
.sap_addr
.itf
= itf
;
112 addr
.sap_addr
.vpi
= 0;
113 addr
.sap_addr
.vci
= vci
;
115 syslog(LOG_INFO
,"Communicating over ATM %d.%d.%d, encapsulation: %s\n", addr
.sap_addr
.itf
,
118 encap
?"VC mux":"LLC");
120 if ((fd
= socket(PF_ATMPVC
, SOCK_DGRAM
, ATM_AAL5
)) < 0)
121 syslog(LOG_ERR
,"failed to create socket %d, reason: %s", errno
,strerror(errno
));
125 qos
.txtp
.traffic_class
= ATM_UBR
;
126 qos
.txtp
.max_sdu
= 1524;
127 qos
.txtp
.pcr
= ATM_MAX_PCR
;
131 if ( (err
=setsockopt(fd
,SOL_SOCKET
,SO_SNDBUF
, &bufsize
,sizeof(bufsize
))) )
132 syslog(LOG_ERR
,"setsockopt SO_SNDBUF: (%d) %s\n",err
, strerror(err
));
134 if (setsockopt(fd
, SOL_ATM
, SO_ATMQOS
, &qos
, sizeof(qos
)) < 0)
135 syslog(LOG_ERR
,"setsockopt SO_ATMQOS %d", errno
);
137 err
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr_atmpvc
));
140 fatal("failed to connect on socket", err
);
142 /* attach the vcc to device: */
144 be
.backend_num
= ATM_BACKEND_BR2684
;
145 be
.ifspec
.method
= BR2684_FIND_BYIFNAME
;
146 sprintf(be
.ifspec
.spec
.ifname
, "nas%d", lastitf
);
147 be
.fcs_in
= BR2684_FCSIN_NO
;
148 be
.fcs_out
= BR2684_FCSOUT_NO
;
150 be
.encaps
= encap
? BR2684_ENCAPS_VC
: BR2684_ENCAPS_LLC
;
154 err
=ioctl (fd
, ATM_SETBACKEND
, &be
);
156 syslog (LOG_INFO
,"Interface configured");
158 syslog (LOG_ERR
,"Could not configure interface:%s",strerror(errno
));
167 printf("usage: %s [-b] [[-c number] [-e 0|1] [-s sndbuf] [-q qos] [-a [itf.]vpi.vci]*]*\n", s
);
173 int main (int argc
, char **argv
)
175 int c
, background
=0, encap
=0, sndbuf
=8192;
176 struct atm_qos reqqos
;
182 memset(&reqqos
, 0, sizeof(reqqos
));
184 openlog (LOG_NAME
,LOG_OPTION
,LOG_FACILITY
);
186 while ((c
= getopt(argc
, argv
,"q:a:bc:e:s:?h")) !=EOF
)
189 printf ("optarg : %s",optarg
);
190 if (text2qos(optarg
,&reqqos
,0)) fprintf(stderr
,"QOS parameter invalid\n");
193 assign_vcc(optarg
, encap
, sndbuf
, reqqos
);
200 itfnum
= atoi(optarg
);
203 encap
=(atoi(optarg
));
205 syslog (LOG_ERR
, "invalid encapsulation: %s:\n",optarg
);
210 sndbuf
=(atoi(optarg
));
212 syslog(LOG_ERR
, "Invalid sndbuf: %s, using size of 8192 instead\n",optarg
);
224 if (argc
!= optind
) usage(argv
[0]);
226 if(lastsock
>=0) close(lastsock
);
233 fprintf(stderr
,"Error detaching\n");
236 exit(0); // This is the parent
238 // Become a process group and session group leader
240 fprintf (stderr
,"Could not set process group\n");
244 // Fork again to let process group leader exit
247 fprintf(stderr
,"Error detaching during second fork\n");
250 exit(0); // This is the parent
252 // Now we're ready for buisness
253 chdir("/"); // Don't keep directories in use
254 close(0); close(1); close(2); // Close stdin, -out and -error
256 Note that this implementation does not keep an open
258 If we need them they can be opened now
263 create_pidfile(itfnum
);
265 syslog (LOG_INFO
, "RFC 1483/2684 bridge daemon started\n");
268 while (1) sleep(30); /* to keep the sockets... */