/* SPDX-License-Identifier: GPL-2.0-or-later */ #include "wire.h" static const uint8_t llc[]={0xe0,0xe0,3},snap[]={0xaa,0xaa,3,0,0,0,0x81,0x37}; int frame_decode(const uint8_t *b,size_t n,int *kind,const uint8_t **p,size_t *len){ if(n<14)return -1; size_t off=14, avail=n-14; uint16_t t=get16(b+12); if(t==0x8137)*kind=IPX_FRAME_ETHERII; else if(t<=1500){if(t>avail)return -1;avail=t; if(avail>=sizeof snap&&!memcmp(b+off,snap,sizeof snap)){*kind=IPX_FRAME_SNAP;off+=sizeof snap;avail-=sizeof snap;} else if(avail>=sizeof llc&&!memcmp(b+off,llc,sizeof llc)){*kind=IPX_FRAME_8022;off+=sizeof llc;avail-=sizeof llc;} else if(avail>=2&&get16(b+off)==0xffff)*kind=IPX_FRAME_8023;else return -1; }else return -1; if(availavail||l>IPX_MAX||b[off+4]>=16)return -1; /* Classic NetWare uses disabled checksum (FFFF). Reject unsupported checksums. */ if(get16(b+off)!=0xffff)return -1; *p=b+off;*len=l;return 0; } int frame_encode(uint8_t *b,size_t cap,int k,const uint8_t *dm,const uint8_t *sm,const uint8_t *p,size_t n){ size_t extra=0;const uint8_t *h=NULL; switch(k){case IPX_FRAME_ETHERII:case IPX_FRAME_8023:break;case IPX_FRAME_8022:extra=sizeof llc;h=llc;break;case IPX_FRAME_SNAP:extra=sizeof snap;h=snap;break;default:return -1;} if(n1500||get16(p+2)!=n)return -1; size_t total=14+extra+n, padded=total<60?60:total;if(capIPX_MAX-IPX_HDR||capsipx_type;write_addr(b+6,dst);write_addr(b+18,src);if(n)memcpy(b+30,data,n);return n+30; }