1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
|
/*
* AX.25 release 034
*
* This is ALPHA test software. This code may break your machine, randomly fail to work with new
* releases, misbehave and/or generally screw up. It might even work.
*
* This code REQUIRES 2.1.10 or higher/ NET3.029
*
* This module:
* This module is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* History
* AX.25 006 Alan(GW4PTS) Nearly died of shock - it's working 8-)
* AX.25 007 Alan(GW4PTS) Removed the silliest bugs
* AX.25 008 Alan(GW4PTS) Cleaned up, fixed a few state machine problems, added callbacks
* AX.25 009 Alan(GW4PTS) Emergency patch kit to fix memory corruption
* AX.25 010 Alan(GW4PTS) Added RAW sockets/Digipeat.
* AX.25 011 Alan(GW4PTS) RAW socket and datagram fixes (thanks) - Raw sendto now gets PID right
* datagram sendto uses correct target address.
* AX.25 012 Alan(GW4PTS) Correct incoming connection handling, send DM to failed connects.
* Use skb->data not skb+1. Support sk->priority correctly.
* Correct receive on SOCK_DGRAM.
* AX.25 013 Alan(GW4PTS) Send DM to all unknown frames, missing initialiser fixed
* Leave spare SSID bits set (DAMA etc) - thanks for bug report,
* removed device registration (it's not used or needed). Clean up for
* gcc 2.5.8. PID to AX25_P_
* AX.25 014 Alan(GW4PTS) Cleanup and NET3 merge
* AX.25 015 Alan(GW4PTS) Internal test version.
* AX.25 016 Alan(GW4PTS) Semi Internal version for PI card
* work.
* AX.25 017 Alan(GW4PTS) Fixed some small bugs reported by
* G4KLX
* AX.25 018 Alan(GW4PTS) Fixed a small error in SOCK_DGRAM
* AX.25 019 Alan(GW4PTS) Clean ups for the non INET kernel and device ioctls in AX.25
* AX.25 020 Jonathan(G4KLX) /proc support and other changes.
* AX.25 021 Alan(GW4PTS) Added AX25_T1, AX25_N2, AX25_T3 as requested.
* AX.25 022 Jonathan(G4KLX) More work on the ax25 auto router and /proc improved (again)!
* Alan(GW4PTS) Added TIOCINQ/OUTQ
* AX.25 023 Alan(GW4PTS) Fixed shutdown bug
* AX.25 023 Alan(GW4PTS) Linus changed timers
* AX.25 024 Alan(GW4PTS) Small bug fixes
* AX.25 025 Alan(GW4PTS) More fixes, Linux 1.1.51 compatibility stuff, timers again!
* AX.25 026 Alan(GW4PTS) Small state fix.
* AX.25 027 Alan(GW4PTS) Socket close crash fixes.
* AX.25 028 Alan(GW4PTS) Callsign control including settings per uid.
* Small bug fixes.
* Protocol set by sockets only.
* Small changes to allow for start of NET/ROM layer.
* AX.25 028a Jonathan(G4KLX) Changes to state machine.
* AX.25 028b Jonathan(G4KLX) Extracted ax25 control block
* from sock structure.
* AX.25 029 Alan(GW4PTS) Combined 028b and some KA9Q code
* Jonathan(G4KLX) and removed all the old Berkeley, added IP mode registration.
* Darryl(G7LED) stuff. Cross-port digipeating. Minor fixes and enhancements.
* Alan(GW4PTS) Missed suser() on axassociate checks
* AX.25 030 Alan(GW4PTS) Added variable length headers.
* Jonathan(G4KLX) Added BPQ Ethernet interface.
* Steven(GW7RRM) Added digi-peating control ioctl.
* Added extended AX.25 support.
* Added AX.25 frame segmentation.
* Darryl(G7LED) Changed connect(), recvfrom(), sendto() sockaddr/addrlen to
* fall inline with bind() and new policy.
* Moved digipeating ctl to new ax25_dev structs.
* Fixed ax25_release(), set TCP_CLOSE, wakeup app
* context, THEN make the sock dead.
* Alan(GW4PTS) Cleaned up for single recvmsg methods.
* Alan(GW4PTS) Fixed not clearing error on connect failure.
* AX.25 031 Jonathan(G4KLX) Added binding to any device.
* Joerg(DL1BKE) Added DAMA support, fixed (?) digipeating, fixed buffer locking
* for "virtual connect" mode... Result: Probably the
* "Most Buggiest Code You've Ever Seen" (TM)
* HaJo(DD8NE) Implementation of a T5 (idle) timer
* Joerg(DL1BKE) Renamed T5 to IDLE and changed behaviour:
* the timer gets reloaded on every received or transmitted
* I frame for IP or NETROM. The idle timer is not active
* on "vanilla AX.25" connections. Furthermore added PACLEN
* to provide AX.25-layer based fragmentation (like WAMPES)
* AX.25 032 Joerg(DL1BKE) Fixed DAMA timeout error.
* ax25_send_frame() limits the number of enqueued
* datagrams per socket.
* AX.25 033 Jonathan(G4KLX) Removed auto-router.
* Hans(PE1AYX) Converted to Module.
* Joerg(DL1BKE) Moved BPQ Ethernet to seperate driver.
* AX.25 034 Jonathan(G4KLX) 2.1 changes
* Alan(GW4PTS) Small POSIXisations
*
* To do:
* Restructure the ax25_rcv code to be cleaner/faster and
* copy only when needed.
* Consider better arbitrary protocol support.
*/
#include <linux/config.h>
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <net/ax25.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/fcntl.h>
#include <linux/termios.h> /* For TIOCINQ/OUTQ */
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/firewall.h>
#include <linux/sysctl.h>
#include <net/ip.h>
#include <net/arp.h>
/*
* The null address is defined as a callsign of all spaces with an
* SSID of zero.
*/
ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
ax25_cb *volatile ax25_list = NULL;
/*
* ax25 -> ascii conversion
*/
char *ax2asc(ax25_address *a)
{
static char buf[11];
char c, *s;
int n;
for (n = 0, s = buf; n < 6; n++) {
c = (a->ax25_call[n] >> 1) & 0x7F;
if (c != ' ') *s++ = c;
}
*s++ = '-';
if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
*s++ = '1';
n -= 10;
}
*s++ = n + '0';
*s++ = '\0';
if (*buf == '\0' || *buf == '-')
return "*";
return buf;
}
/*
* ascii -> ax25 conversion
*/
ax25_address *asc2ax(char *callsign)
{
static ax25_address addr;
char *s;
int n;
for (s = callsign, n = 0; n < 6; n++) {
if (*s != '\0' && *s != '-')
addr.ax25_call[n] = *s++;
else
addr.ax25_call[n] = ' ';
addr.ax25_call[n] <<= 1;
addr.ax25_call[n] &= 0xFE;
}
if (*s++ == '\0') {
addr.ax25_call[6] = 0x00;
return &addr;
}
addr.ax25_call[6] = *s++ - '0';
if (*s != '\0') {
addr.ax25_call[6] *= 10;
addr.ax25_call[6] += *s++ - '0';
}
addr.ax25_call[6] <<= 1;
addr.ax25_call[6] &= 0x1E;
return &addr;
}
/*
* Compare two ax.25 addresses
*/
int ax25cmp(ax25_address *a, ax25_address *b)
{
int ct = 0;
while (ct < 6) {
if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
return 1;
ct++;
}
if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
return 0;
return 2; /* Partial match */
}
/*
* Free an allocated ax25 control block. This is done to centralise
* the MOD count code.
*/
static void ax25_free_cb(ax25_cb *ax25)
{
if (ax25->digipeat != NULL) {
kfree_s(ax25->digipeat, sizeof(ax25_digi));
ax25->digipeat = NULL;
}
kfree_s(ax25, sizeof(ax25_cb));
MOD_DEC_USE_COUNT;
}
/*
* Socket removal during an interrupt is now safe.
*/
static void ax25_remove_socket(ax25_cb *ax25)
{
ax25_cb *s;
unsigned long flags;
save_flags(flags);
cli();
if ((s = ax25_list) == ax25) {
ax25_list = s->next;
restore_flags(flags);
return;
}
while (s != NULL && s->next != NULL) {
if (s->next == ax25) {
s->next = ax25->next;
restore_flags(flags);
return;
}
s = s->next;
}
restore_flags(flags);
}
/*
* Kill all bound sockets on a dropped device.
*/
static void ax25_kill_by_device(struct device *dev)
{
ax25_cb *s;
for (s = ax25_list; s != NULL; s = s->next) {
if (s->device == dev) {
s->state = AX25_STATE_0;
s->device = NULL;
if (s->sk != NULL) {
s->sk->state = TCP_CLOSE;
s->sk->err = ENETUNREACH;
s->sk->shutdown |= SEND_SHUTDOWN;
if (!s->sk->dead)
s->sk->state_change(s->sk);
s->sk->dead = 1;
}
}
}
}
/*
* Handle device status changes.
*/
static int ax25_device_event(struct notifier_block *this,unsigned long event, void *ptr)
{
struct device *dev = (struct device *)ptr;
/* Reject non AX.25 devices */
if (dev->type != ARPHRD_AX25)
return NOTIFY_DONE;
switch (event) {
case NETDEV_UP:
ax25_dev_device_up(dev);
break;
case NETDEV_DOWN:
ax25_kill_by_device(dev);
ax25_rt_device_down(dev);
ax25_dev_device_down(dev);
break;
default:
break;
}
return NOTIFY_DONE;
}
/*
* Add a socket to the bound sockets list.
*/
static void ax25_insert_socket(ax25_cb *ax25)
{
unsigned long flags;
save_flags(flags);
cli();
ax25->next = ax25_list;
ax25_list = ax25;
restore_flags(flags);
}
/*
* Find a socket that wants to accept the SABM we have just
* received.
*/
static struct sock *ax25_find_listener(ax25_address *addr, struct device *dev, int type)
{
unsigned long flags;
ax25_cb *s;
save_flags(flags);
cli();
for (s = ax25_list; s != NULL; s = s->next) {
if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == type && s->sk->state == TCP_LISTEN) {
/* If device is null we match any device */
if (s->device == NULL || s->device == dev) {
restore_flags(flags);
return s->sk;
}
}
}
restore_flags(flags);
return NULL;
}
/*
* Find an AX.25 socket given both ends.
*/
static struct sock *ax25_find_socket(ax25_address *my_addr, ax25_address *dest_addr, int type)
{
ax25_cb *s;
unsigned long flags;
save_flags(flags);
cli();
for (s = ax25_list; s != NULL; s = s->next) {
if (s->sk != NULL && ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->sk->type == type) {
restore_flags(flags);
return s->sk;
}
}
restore_flags(flags);
return NULL;
}
/*
* Find an AX.25 control block given both ends. It will only pick up
* floating AX.25 control blocks or non Raw socket bound control blocks.
*/
static ax25_cb *ax25_find_cb(ax25_address *my_addr, ax25_address *dest_addr, struct device *dev)
{
ax25_cb *s;
unsigned long flags;
save_flags(flags);
cli();
for (s = ax25_list; s != NULL; s = s->next) {
if (s->sk != NULL && s->sk->type != SOCK_SEQPACKET)
continue;
if (ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->device == dev) {
restore_flags(flags);
return s;
}
}
restore_flags(flags);
return NULL;
}
/*
* Look for any matching address - RAW sockets can bind to arbitrary names
*/
static struct sock *ax25_addr_match(ax25_address *addr)
{
unsigned long flags;
ax25_cb *s;
save_flags(flags);
cli();
for (s = ax25_list; s != NULL; s = s->next) {
if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == SOCK_RAW) {
restore_flags(flags);
return s->sk;
}
}
restore_flags(flags);
return NULL;
}
static void ax25_send_to_raw(struct sock *sk, struct sk_buff *skb, int proto)
{
struct sk_buff *copy;
while (sk != NULL) {
if (sk->type == SOCK_RAW && sk->protocol == proto && sk->rmem_alloc <= sk->rcvbuf) {
if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
return;
copy->sk = sk;
atomic_add(copy->truesize, &sk->rmem_alloc);
skb_queue_tail(&sk->receive_queue, copy);
if (!sk->dead)
sk->data_ready(sk, skb->len);
}
sk = sk->next;
}
}
/*
* Deferred destroy.
*/
void ax25_destroy_socket(ax25_cb *);
/*
* Handler for deferred kills.
*/
static void ax25_destroy_timer(unsigned long data)
{
ax25_destroy_socket((ax25_cb *)data);
}
/*
* This is called from user mode and the timers. Thus it protects itself against
* interrupt users but doesn't worry about being called during work.
* Once it is removed from the queue no interrupt or bottom half will
* touch it and we are (fairly 8-) ) safe.
*/
void ax25_destroy_socket(ax25_cb *ax25) /* Not static as it's used by the timer */
{
struct sk_buff *skb;
unsigned long flags;
save_flags(flags);
cli();
del_timer(&ax25->timer);
ax25_remove_socket(ax25);
ax25_clear_queues(ax25); /* Flush the queues */
if (ax25->sk != NULL) {
while ((skb = skb_dequeue(&ax25->sk->receive_queue)) != NULL) {
if (skb->sk != ax25->sk) { /* A pending connection */
skb->sk->dead = 1; /* Queue the unaccepted socket for death */
ax25_set_timer(skb->sk->protinfo.ax25);
skb->sk->protinfo.ax25->state = AX25_STATE_0;
}
kfree_skb(skb, FREE_READ);
}
}
if (ax25->sk != NULL) {
if (ax25->sk->wmem_alloc || ax25->sk->rmem_alloc) { /* Defer: outstanding buffers */
init_timer(&ax25->timer);
ax25->timer.expires = jiffies + 10 * HZ;
ax25->timer.function = ax25_destroy_timer;
ax25->timer.data = (unsigned long)ax25;
add_timer(&ax25->timer);
} else {
sk_free(ax25->sk);
ax25_free_cb(ax25);
}
} else {
ax25_free_cb(ax25);
}
restore_flags(flags);
}
/*
* Callsign/UID mapper. This is in kernel space for security on multi-amateur machines.
*/
ax25_uid_assoc *ax25_uid_list;
int ax25_uid_policy = 0;
ax25_address *ax25_findbyuid(uid_t uid)
{
ax25_uid_assoc *a;
for (a = ax25_uid_list; a != NULL; a = a->next) {
if (a->uid == uid)
return &a->call;
}
return NULL;
}
static int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax)
{
ax25_uid_assoc *a;
switch (cmd) {
case SIOCAX25GETUID:
for (a = ax25_uid_list; a != NULL; a = a->next) {
if (ax25cmp(&sax->sax25_call, &a->call) == 0)
return a->uid;
}
return -ENOENT;
case SIOCAX25ADDUID:
if (!suser())
return -EPERM;
if (ax25_findbyuid(sax->sax25_uid))
return -EEXIST;
if (sax->sax25_uid == 0)
return -EINVAL;
a = (ax25_uid_assoc *)kmalloc(sizeof(*a), GFP_KERNEL);
if (a == NULL)
return -ENOMEM;
a->uid = sax->sax25_uid;
a->call = sax->sax25_call;
a->next = ax25_uid_list;
ax25_uid_list = a;
return 0;
case SIOCAX25DELUID: {
ax25_uid_assoc **l;
if (!suser())
return -EPERM;
l = &ax25_uid_list;
while ((*l) != NULL) {
if (ax25cmp(&((*l)->call), &(sax->sax25_call)) == 0) {
a = *l;
*l = (*l)->next;
kfree_s(a, sizeof(*a));
return 0;
}
l = &((*l)->next);
}
return -ENOENT;
}
default:
return -EINVAL;
}
return -EINVAL; /*NOTREACHED */
}
/*
* dl1bke 960311: set parameters for existing AX.25 connections,
* includes a KILL command to abort any connection.
* VERY useful for debugging ;-)
*/
static int ax25_ctl_ioctl(const unsigned int cmd, void *arg)
{
struct ax25_ctl_struct ax25_ctl;
struct device *dev;
ax25_cb *ax25;
unsigned long flags;
int err;
if ((err = verify_area(VERIFY_READ, arg, sizeof(ax25_ctl))) != 0)
return err;
copy_from_user(&ax25_ctl, arg, sizeof(ax25_ctl));
if ((dev = ax25rtr_get_dev(&ax25_ctl.port_addr)) == NULL)
return -ENODEV;
if ((ax25 = ax25_find_cb(&ax25_ctl.source_addr, &ax25_ctl.dest_addr, dev)) == NULL)
return -ENOTCONN;
switch (ax25_ctl.cmd) {
case AX25_KILL:
ax25_clear_queues(ax25);
ax25_send_control(ax25, DISC, POLLON, C_COMMAND);
ax25->state = AX25_STATE_0;
if (ax25->sk != NULL) {
ax25->sk->state = TCP_CLOSE;
ax25->sk->err = ENETRESET;
ax25->sk->shutdown |= SEND_SHUTDOWN;
if (!ax25->sk->dead)
ax25->sk->state_change(ax25->sk);
ax25->sk->dead = 1;
}
ax25_dama_off(ax25);
ax25_set_timer(ax25);
break;
case AX25_WINDOW:
if (ax25->modulus == MODULUS) {
if (ax25_ctl.arg < 1 || ax25_ctl.arg > 7)
return -EINVAL;
} else {
if (ax25_ctl.arg < 1 || ax25_ctl.arg > 63)
return -EINVAL;
}
ax25->window = ax25_ctl.arg;
break;
case AX25_T1:
if (ax25_ctl.arg < 1)
return -EINVAL;
ax25->rtt = (ax25_ctl.arg * PR_SLOWHZ) / 2;
ax25->t1 = ax25_ctl.arg * PR_SLOWHZ;
save_flags(flags); cli();
if (ax25->t1timer > ax25->t1)
ax25->t1timer = ax25->t1;
restore_flags(flags);
break;
case AX25_T2:
if (ax25_ctl.arg < 1)
return -EINVAL;
save_flags(flags); cli();
ax25->t2 = ax25_ctl.arg * PR_SLOWHZ;
if (ax25->t2timer > ax25->t2)
ax25->t2timer = ax25->t2;
restore_flags(flags);
break;
case AX25_N2:
if (ax25_ctl.arg < 1 || ax25_ctl.arg > 31)
return -EINVAL;
ax25->n2count = 0;
ax25->n2 = ax25_ctl.arg;
break;
case AX25_T3:
if (ax25_ctl.arg < 0)
return -EINVAL;
save_flags(flags); cli();
ax25->t3 = ax25_ctl.arg * PR_SLOWHZ;
if (ax25->t3timer != 0)
ax25->t3timer = ax25->t3;
restore_flags(flags);
break;
case AX25_IDLE:
if (ax25_ctl.arg < 0)
return -EINVAL;
save_flags(flags); cli();
ax25->idle = ax25_ctl.arg * PR_SLOWHZ * 60;
if (ax25->idletimer != 0)
ax25->idletimer = ax25->idle;
restore_flags(flags);
break;
case AX25_PACLEN:
if (ax25_ctl.arg < 16 || ax25_ctl.arg > 65535)
return -EINVAL;
#if 0
if (ax25_ctl.arg > 256) /* we probably want this */
printk(KERN_WARNING "ax25_ctl_ioctl: Warning --- huge paclen %d\n", (int)ax25_ctl.arg);
#endif
ax25->paclen = ax25_ctl.arg;
break;
case AX25_MAXQUEUE:
if (ax25_ctl.arg < 1)
return -EINVAL;
ax25->maxqueue = ax25_ctl.arg;
break;
default:
return -EINVAL;
}
return 0;
}
/*
* Create an empty AX.25 control block.
*/
static ax25_cb *ax25_create_cb(void)
{
ax25_cb *ax25;
if ((ax25 = (ax25_cb *)kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
return NULL;
MOD_INC_USE_COUNT;
skb_queue_head_init(&ax25->write_queue);
skb_queue_head_init(&ax25->frag_queue);
skb_queue_head_init(&ax25->ack_queue);
skb_queue_head_init(&ax25->reseq_queue);
init_timer(&ax25->timer);
ax25->dama_slave = 0;
ax25->rtt = AX25_DEF_T1 / 2;
ax25->t1 = AX25_DEF_T1;
ax25->t2 = AX25_DEF_T2;
ax25->t3 = AX25_DEF_T3;
ax25->n2 = AX25_DEF_N2;
ax25->paclen = AX25_DEF_PACLEN;
ax25->maxqueue= AX25_DEF_MAXQUEUE;
ax25->idle = AX25_DEF_IDLE;
if (AX25_DEF_AXDEFMODE) {
ax25->modulus = EMODULUS;
ax25->window = AX25_DEF_EWINDOW;
} else {
ax25->modulus = MODULUS;
ax25->window = AX25_DEF_WINDOW;
}
ax25->fragno = 0;
ax25->fraglen = 0;
ax25->hdrincl = 0;
ax25->backoff = AX25_DEF_BACKOFF;
ax25->condition = 0x00;
ax25->t1timer = 0;
ax25->t2timer = 0;
ax25->t3timer = 0;
ax25->n2count = 0;
ax25->idletimer = 0;
ax25->va = 0;
ax25->vr = 0;
ax25->vs = 0;
ax25->device = NULL;
ax25->digipeat = NULL;
ax25->sk = NULL;
ax25->state = AX25_STATE_0;
memset(&ax25->dest_addr, '\0', AX25_ADDR_LEN);
memset(&ax25->source_addr, '\0', AX25_ADDR_LEN);
return ax25;
}
/*
* Find out if we are a DAMA slave for this device and count the
* number of connections.
*
* dl1bke 951121
*/
int ax25_dev_is_dama_slave(struct device *dev)
{
ax25_cb *ax25;
int count = 0;
for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
if (ax25->device == dev && ax25->dama_slave) {
count++;
break;
}
}
return count;
}
/*
* Fill in a created AX.25 created control block with the default
* values for a particular device.
*/
static void ax25_fillin_cb(ax25_cb *ax25, struct device *dev)
{
ax25->device = dev;
ax25->rtt = ax25_dev_get_value(dev, AX25_VALUES_T1);
ax25->t1 = ax25_dev_get_value(dev, AX25_VALUES_T1);
ax25->t2 = ax25_dev_get_value(dev, AX25_VALUES_T2);
ax25->t3 = ax25_dev_get_value(dev, AX25_VALUES_T3);
ax25->n2 = ax25_dev_get_value(dev, AX25_VALUES_N2);
ax25->paclen = ax25_dev_get_value(dev, AX25_VALUES_PACLEN);
ax25->maxqueue = ax25_dev_get_value(dev, AX25_VALUES_MAXQUEUE);
ax25->idle = ax25_dev_get_value(dev, AX25_VALUES_IDLE);
ax25->dama_slave = 0;
if (ax25_dev_get_value(dev, AX25_VALUES_AXDEFMODE)) {
ax25->modulus = EMODULUS;
ax25->window = ax25_dev_get_value(dev, AX25_VALUES_EWINDOW);
} else {
ax25->modulus = MODULUS;
ax25->window = ax25_dev_get_value(dev, AX25_VALUES_WINDOW);
}
ax25->backoff = ax25_dev_get_value(dev, AX25_VALUES_BACKOFF);
}
int ax25_send_frame(struct sk_buff *skb, ax25_address *src, ax25_address *dest,
ax25_digi *digi, struct device *dev)
{
ax25_cb *ax25;
if (skb == NULL)
return 0;
/*
* Look for an existing connection.
*/
for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
if (ax25->sk != NULL && ax25->sk->type != SOCK_SEQPACKET)
continue;
if (ax25cmp(&ax25->source_addr, src) == 0 && ax25cmp(&ax25->dest_addr, dest) == 0 && ax25->device == dev) {
if (ax25_queue_length(ax25, skb) > ax25->maxqueue * ax25->window) {
kfree_skb(skb, FREE_WRITE);
} else {
ax25_output(ax25, skb);
}
ax25->idletimer = ax25->idle;
return 1; /* It already existed */
}
}
if ((ax25 = ax25_create_cb()) == NULL)
return 0;
ax25_fillin_cb(ax25, dev);
ax25->source_addr = *src;
ax25->dest_addr = *dest;
if (digi != NULL) {
if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
ax25_free_cb(ax25);
return 0;
}
*ax25->digipeat = *digi;
} else {
ax25_rt_build_path(ax25, dest, dev);
}
if (ax25_dev_is_dama_slave(ax25->device))
dama_establish_data_link(ax25);
else
ax25_establish_data_link(ax25);
/* idle timeouts only for mode vc connections */
ax25->idletimer = ax25->idle;
ax25_insert_socket(ax25);
ax25->state = AX25_STATE_1;
ax25_set_timer(ax25);
ax25_output(ax25, skb);
return 1; /* We had to create it */
}
/*
* Return the state of an AX.25 link given source, destination, and
* device.
*/
int ax25_link_up(ax25_address *src, ax25_address *dest, struct device *dev)
{
ax25_cb *ax25;
for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
if (ax25->sk != NULL && ax25->sk->type != SOCK_SEQPACKET)
continue;
if (ax25cmp(&ax25->source_addr, src) == 0 && ax25cmp(&ax25->dest_addr, dest) == 0 && ax25->device == dev)
return 1;
}
return 0;
}
/*
* Find the AX.25 device that matches the hardware address supplied.
*/
struct device *ax25rtr_get_dev(ax25_address *addr)
{
struct device *dev;
for (dev = dev_base; dev != NULL; dev = dev->next)
if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25 &&
ax25cmp(addr, (ax25_address*) dev->dev_addr) == 0)
return dev;
return NULL;
}
/*
* Handling for system calls applied via the various interfaces to an
* AX25 socket object
*/
static int ax25_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
return -EINVAL;
}
static int ax25_setsockopt(struct socket *sock, int level, int optname,
char *optval, int optlen)
{
struct sock *sk;
int err, opt;
sk = (struct sock *)sock->data;
if (level == SOL_SOCKET)
return sock_setsockopt(sk, level, optname, optval, optlen);
if (level != SOL_AX25)
return -EOPNOTSUPP;
if (optval == NULL)
return -EINVAL;
if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
return err;
get_user(opt, (int *)optval);
switch (optname) {
case AX25_WINDOW:
if (sk->protinfo.ax25->modulus == MODULUS) {
if (opt < 1 || opt > 7)
return -EINVAL;
} else {
if (opt < 1 || opt > 63)
return -EINVAL;
}
sk->protinfo.ax25->window = opt;
return 0;
case AX25_T1:
if (opt < 1)
return -EINVAL;
sk->protinfo.ax25->rtt = (opt * PR_SLOWHZ) / 2;
return 0;
case AX25_T2:
if (opt < 1)
return -EINVAL;
sk->protinfo.ax25->t2 = opt * PR_SLOWHZ;
return 0;
case AX25_N2:
if (opt < 1 || opt > 31)
return -EINVAL;
sk->protinfo.ax25->n2 = opt;
return 0;
case AX25_T3:
if (opt < 1)
return -EINVAL;
sk->protinfo.ax25->t3 = opt * PR_SLOWHZ;
return 0;
case AX25_IDLE:
if (opt < 0)
return -EINVAL;
sk->protinfo.ax25->idle = opt * PR_SLOWHZ * 60;
return 0;
case AX25_BACKOFF:
sk->protinfo.ax25->backoff = opt ? 1 : 0;
return 0;
case AX25_EXTSEQ:
sk->protinfo.ax25->modulus = opt ? EMODULUS : MODULUS;
return 0;
case AX25_HDRINCL:
sk->protinfo.ax25->hdrincl = opt ? 1 : 0;
return 0;
case AX25_PACLEN:
if (opt < 16 || opt > 65535)
return -EINVAL;
sk->protinfo.ax25->paclen = opt;
return 0;
default:
return -ENOPROTOOPT;
}
}
static int ax25_getsockopt(struct socket *sock, int level, int optname,
char *optval, int *optlen)
{
struct sock *sk;
int val = 0;
int err;
sk = (struct sock *)sock->data;
if (level == SOL_SOCKET)
return sock_getsockopt(sk, level, optname, optval, optlen);
if (level != SOL_AX25)
return -EOPNOTSUPP;
switch (optname) {
case AX25_WINDOW:
val = sk->protinfo.ax25->window;
break;
case AX25_T1:
val = (sk->protinfo.ax25->t1 * 2) / PR_SLOWHZ;
break;
case AX25_T2:
val = sk->protinfo.ax25->t2 / PR_SLOWHZ;
break;
case AX25_N2:
val = sk->protinfo.ax25->n2;
break;
case AX25_T3:
val = sk->protinfo.ax25->t3 / PR_SLOWHZ;
break;
case AX25_IDLE:
val = sk->protinfo.ax25->idle / (PR_SLOWHZ * 60);
break;
case AX25_BACKOFF:
val = sk->protinfo.ax25->backoff;
break;
case AX25_EXTSEQ:
val = (sk->protinfo.ax25->modulus == EMODULUS);
break;
case AX25_HDRINCL:
val = sk->protinfo.ax25->hdrincl;
break;
case AX25_PACLEN:
val = sk->protinfo.ax25->paclen;
break;
default:
return -ENOPROTOOPT;
}
if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
return err;
put_user(sizeof(int), optlen);
if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
return err;
put_user(val, (int *) optval);
return 0;
}
static int ax25_listen(struct socket *sock, int backlog)
{
struct sock *sk = (struct sock *)sock->data;
if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
sk->max_ack_backlog = backlog;
sk->state = TCP_LISTEN;
return 0;
}
return -EOPNOTSUPP;
}
static void def_callback1(struct sock *sk)
{
if (!sk->dead)
wake_up_interruptible(sk->sleep);
}
static void def_callback2(struct sock *sk, int len)
{
if (!sk->dead)
wake_up_interruptible(sk->sleep);
}
static int ax25_create(struct socket *sock, int protocol)
{
struct sock *sk;
ax25_cb *ax25;
switch (sock->type) {
case SOCK_DGRAM:
if (protocol == 0 || protocol == AF_AX25)
protocol = AX25_P_TEXT;
break;
case SOCK_SEQPACKET:
switch (protocol) {
case 0:
case AF_AX25: /* For CLX */
protocol = AX25_P_TEXT;
break;
case AX25_P_SEGMENT:
#ifdef CONFIG_INET
case AX25_P_ARP:
case AX25_P_IP:
#endif
#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
case AX25_P_NETROM:
#endif
#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
case AX25_P_ROSE:
#endif
return -ESOCKTNOSUPPORT;
default:
break;
}
break;
case SOCK_RAW:
break;
default:
return -ESOCKTNOSUPPORT;
}
if ((sk = sk_alloc(GFP_ATOMIC)) == NULL)
return -ENOMEM;
if ((ax25 = ax25_create_cb()) == NULL) {
sk_free(sk);
return -ENOMEM;
}
skb_queue_head_init(&sk->receive_queue);
skb_queue_head_init(&sk->write_queue);
skb_queue_head_init(&sk->back_log);
sk->socket = sock;
sk->type = sock->type;
sk->protocol = protocol;
sk->next = NULL;
sk->allocation = GFP_KERNEL;
sk->rcvbuf = SK_RMEM_MAX;
sk->sndbuf = SK_WMEM_MAX;
sk->state = TCP_CLOSE;
sk->priority = SOPRI_NORMAL;
sk->mtu = AX25_MTU; /* 256 */
sk->zapped = 1;
sk->state_change = def_callback1;
sk->data_ready = def_callback2;
sk->write_space = def_callback1;
sk->error_report = def_callback1;
if (sock != NULL) {
sock->data = (void *)sk;
sk->sleep = sock->wait;
}
ax25->sk = sk;
sk->protinfo.ax25 = ax25;
return 0;
}
static struct sock *ax25_make_new(struct sock *osk, struct device *dev)
{
struct sock *sk;
ax25_cb *ax25;
if ((sk = sk_alloc(GFP_ATOMIC)) == NULL)
return NULL;
if ((ax25 = ax25_create_cb()) == NULL) {
sk_free(sk);
return NULL;
}
ax25_fillin_cb(ax25, dev);
sk->type = osk->type;
sk->socket = osk->socket;
switch (osk->type) {
case SOCK_DGRAM:
break;
case SOCK_SEQPACKET:
break;
default:
sk_free(sk);
ax25_free_cb(ax25);
return NULL;
}
skb_queue_head_init(&sk->receive_queue);
skb_queue_head_init(&sk->write_queue);
skb_queue_head_init(&sk->back_log);
sk->next = NULL;
sk->priority = osk->priority;
sk->protocol = osk->protocol;
sk->rcvbuf = osk->rcvbuf;
sk->sndbuf = osk->sndbuf;
sk->debug = osk->debug;
sk->state = TCP_ESTABLISHED;
sk->window = osk->window;
sk->mtu = osk->mtu;
sk->sleep = osk->sleep;
sk->zapped = osk->zapped;
sk->state_change = def_callback1;
sk->data_ready = def_callback2;
sk->write_space = def_callback1;
sk->error_report = def_callback1;
ax25->modulus = osk->protinfo.ax25->modulus;
ax25->backoff = osk->protinfo.ax25->backoff;
ax25->hdrincl = osk->protinfo.ax25->hdrincl;
ax25->rtt = osk->protinfo.ax25->rtt;
ax25->t1 = osk->protinfo.ax25->t1;
ax25->t2 = osk->protinfo.ax25->t2;
ax25->t3 = osk->protinfo.ax25->t3;
ax25->n2 = osk->protinfo.ax25->n2;
ax25->idle = osk->protinfo.ax25->idle;
ax25->paclen = osk->protinfo.ax25->paclen;
ax25->window = osk->protinfo.ax25->window;
ax25->maxqueue = osk->protinfo.ax25->maxqueue;
ax25->source_addr = osk->protinfo.ax25->source_addr;
if (osk->protinfo.ax25->digipeat != NULL) {
if ((ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
sk_free(sk);
ax25_free_cb(ax25);
return NULL;
}
*ax25->digipeat = *osk->protinfo.ax25->digipeat;
}
sk->protinfo.ax25 = ax25;
ax25->sk = sk;
return sk;
}
static int ax25_dup(struct socket *newsock, struct socket *oldsock)
{
struct sock *sk = (struct sock *)oldsock->data;
return ax25_create(newsock, sk->protocol);
}
static int ax25_release(struct socket *sock, struct socket *peer)
{
struct sock *sk = (struct sock *)sock->data;
if (sk == NULL) return 0;
if (sk->type == SOCK_SEQPACKET) {
switch (sk->protinfo.ax25->state) {
case AX25_STATE_0:
sk->state = TCP_CLOSE;
sk->shutdown |= SEND_SHUTDOWN;
sk->state_change(sk);
sk->dead = 1;
ax25_destroy_socket(sk->protinfo.ax25);
break;
case AX25_STATE_1:
ax25_send_control(sk->protinfo.ax25, DISC, POLLON, C_COMMAND);
sk->protinfo.ax25->state = AX25_STATE_0;
sk->state = TCP_CLOSE;
sk->shutdown |= SEND_SHUTDOWN;
sk->state_change(sk);
sk->dead = 1;
ax25_destroy_socket(sk->protinfo.ax25);
break;
case AX25_STATE_2:
if (sk->protinfo.ax25->dama_slave)
ax25_send_control(sk->protinfo.ax25, DISC, POLLON, C_COMMAND);
else
ax25_send_control(sk->protinfo.ax25, DM, POLLON, C_RESPONSE);
sk->protinfo.ax25->state = AX25_STATE_0;
sk->state = TCP_CLOSE;
sk->shutdown |= SEND_SHUTDOWN;
sk->state_change(sk);
sk->dead = 1;
ax25_destroy_socket(sk->protinfo.ax25);
break;
case AX25_STATE_3:
case AX25_STATE_4:
ax25_clear_queues(sk->protinfo.ax25);
sk->protinfo.ax25->n2count = 0;
if (!sk->protinfo.ax25->dama_slave) {
ax25_send_control(sk->protinfo.ax25, DISC, POLLON, C_COMMAND);
sk->protinfo.ax25->t3timer = 0;
} else {
sk->protinfo.ax25->t3timer = sk->protinfo.ax25->t3; /* DAMA slave timeout */
}
sk->protinfo.ax25->t1timer = sk->protinfo.ax25->t1 = ax25_calculate_t1(sk->protinfo.ax25);
sk->protinfo.ax25->state = AX25_STATE_2;
sk->state = TCP_CLOSE;
sk->shutdown |= SEND_SHUTDOWN;
sk->state_change(sk);
sk->dead = 1;
sk->destroy = 1;
break;
default:
break;
}
} else {
sk->state = TCP_CLOSE;
sk->shutdown |= SEND_SHUTDOWN;
sk->state_change(sk);
sk->dead = 1;
ax25_destroy_socket(sk->protinfo.ax25);
}
sock->data = NULL;
sk->socket = NULL; /* Not used, but we should do this. **/
return 0;
}
/*
* We support a funny extension here so you can (as root) give any callsign
* digipeated via a local address as source. This is a hack until we add
* BSD 4.4 ADDIFADDR type support. It is however small and trivially backward
* compatible 8)
*/
static int ax25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
struct sock *sk;
struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
struct device *dev;
ax25_address *call;
sk = (struct sock *)sock->data;
if (sk->zapped == 0)
return -EINVAL;
if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
return -EINVAL;
call = ax25_findbyuid(current->euid);
if (call == NULL && ax25_uid_policy && !suser())
return -EACCES;
if (call == NULL)
sk->protinfo.ax25->source_addr = addr->fsa_ax25.sax25_call;
else
sk->protinfo.ax25->source_addr = *call;
if (sk->debug)
printk("AX25: source address set to %s\n", ax2asc(&sk->protinfo.ax25->source_addr));
if (addr_len == sizeof(struct full_sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
if (ax25cmp(&addr->fsa_digipeater[0], &null_ax25_address) == 0) {
dev = NULL;
if (sk->debug)
printk("AX25: bound to any device\n");
} else {
if ((dev = ax25rtr_get_dev(&addr->fsa_digipeater[0])) == NULL) {
if (sk->debug)
printk("AX25: bind failed - no device\n");
return -EADDRNOTAVAIL;
}
if (sk->debug)
printk("AX25: bound to device %s\n", dev->name);
}
} else {
if ((dev = ax25rtr_get_dev(&addr->fsa_ax25.sax25_call)) == NULL) {
if (sk->debug)
printk("AX25: bind failed - no device\n");
return -EADDRNOTAVAIL;
}
if (sk->debug)
printk("AX25: bound to device %s\n", dev->name);
}
ax25_fillin_cb(sk->protinfo.ax25, dev);
ax25_insert_socket(sk->protinfo.ax25);
sk->zapped = 0;
if (sk->debug)
printk("AX25: socket is bound\n");
return 0;
}
static int ax25_connect(struct socket *sock, struct sockaddr *uaddr,
int addr_len, int flags)
{
struct sock *sk = (struct sock *)sock->data;
struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
int err;
if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
sock->state = SS_CONNECTED;
return 0; /* Connect completed during a ERESTARTSYS event */
}
if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
sock->state = SS_UNCONNECTED;
return -ECONNREFUSED;
}
if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
return -EISCONN; /* No reconnect on a seqpacket socket */
sk->state = TCP_CLOSE;
sock->state = SS_UNCONNECTED;
if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
return -EINVAL;
/*
* Handle digi-peaters to be used.
*/
if (addr_len == sizeof(struct full_sockaddr_ax25) && addr->sax25_ndigis != 0) {
int ct = 0;
struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)addr;
/* Valid number of digipeaters ? */
if (addr->sax25_ndigis < 1 || addr->sax25_ndigis > AX25_MAX_DIGIS)
return -EINVAL;
if (sk->protinfo.ax25->digipeat == NULL) {
if ((sk->protinfo.ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL)
return -ENOBUFS;
}
sk->protinfo.ax25->digipeat->ndigi = addr->sax25_ndigis;
while (ct < addr->sax25_ndigis) {
sk->protinfo.ax25->digipeat->repeated[ct] = 0;
sk->protinfo.ax25->digipeat->calls[ct] = fsa->fsa_digipeater[ct];
ct++;
}
sk->protinfo.ax25->digipeat->lastrepeat = 0;
}
/*
* Must bind first - autobinding in this may or may not work. If
* the socket is already bound, check to see if the device has
* been filled in, error if it hasn't.
*/
if (sk->zapped) {
if ((err = ax25_rt_autobind(sk->protinfo.ax25, &addr->sax25_call)) < 0)
return err;
ax25_fillin_cb(sk->protinfo.ax25, sk->protinfo.ax25->device);
ax25_insert_socket(sk->protinfo.ax25);
} else {
if (sk->protinfo.ax25->device == NULL)
return -EHOSTUNREACH;
}
if (sk->type == SOCK_SEQPACKET && ax25_find_cb(&sk->protinfo.ax25->source_addr, &addr->sax25_call, sk->protinfo.ax25->device) != NULL)
return -EADDRINUSE; /* Already such a connection */
sk->protinfo.ax25->dest_addr = addr->sax25_call;
/* First the easy one */
if (sk->type != SOCK_SEQPACKET) {
sock->state = SS_CONNECTED;
sk->state = TCP_ESTABLISHED;
return 0;
}
/* Move to connecting socket, ax.25 lapb WAIT_UA.. */
sock->state = SS_CONNECTING;
sk->state = TCP_SYN_SENT;
if (ax25_dev_is_dama_slave(sk->protinfo.ax25->device))
dama_establish_data_link(sk->protinfo.ax25);
else
ax25_establish_data_link(sk->protinfo.ax25);
sk->protinfo.ax25->state = AX25_STATE_1;
ax25_set_timer(sk->protinfo.ax25); /* Start going SABM SABM until a UA or a give up and DM */
/* Now the loop */
if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
return -EINPROGRESS;
cli(); /* To avoid races on the sleep */
/* A DM or timeout will go to closed, a UA will go to ABM */
while (sk->state == TCP_SYN_SENT) {
interruptible_sleep_on(sk->sleep);
if (current->signal & ~current->blocked) {
sti();
return -ERESTARTSYS;
}
}
if (sk->state != TCP_ESTABLISHED) {
/* Not in ABM, not in WAIT_UA -> failed */
sti();
sock->state = SS_UNCONNECTED;
return sock_error(sk); /* Always set at this point */
}
sock->state = SS_CONNECTED;
sti();
return 0;
}
static int ax25_socketpair(struct socket *sock1, struct socket *sock2)
{
return -EOPNOTSUPP;
}
static int ax25_accept(struct socket *sock, struct socket *newsock, int flags)
{
struct sock *sk;
struct sock *newsk;
struct sk_buff *skb;
if (newsock->data)
sk_free(newsock->data);
newsock->data = NULL;
sk = (struct sock *)sock->data;
if (sk->type != SOCK_SEQPACKET)
return -EOPNOTSUPP;
if (sk->state != TCP_LISTEN)
return -EINVAL;
/*
* The write queue this time is holding sockets ready to use
* hooked into the SABM we saved
*/
do {
cli();
if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
if (flags & O_NONBLOCK) {
sti();
return 0;
}
interruptible_sleep_on(sk->sleep);
if (current->signal & ~current->blocked) {
sti();
return -ERESTARTSYS;
}
}
} while (skb == NULL);
newsk = skb->sk;
newsk->pair = NULL;
sti();
/* Now attach up the new socket */
skb->sk = NULL;
kfree_skb(skb, FREE_READ);
sk->ack_backlog--;
newsock->data = newsk;
return 0;
}
static int ax25_getname(struct socket *sock, struct sockaddr *uaddr,
int *uaddr_len, int peer)
{
struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
struct sock *sk;
unsigned char ndigi, i;
sk = (struct sock *)sock->data;
if (peer != 0) {
if (sk->state != TCP_ESTABLISHED)
return -ENOTCONN;
sax->fsa_ax25.sax25_family = AF_AX25;
sax->fsa_ax25.sax25_call = sk->protinfo.ax25->dest_addr;
sax->fsa_ax25.sax25_ndigis = 0;
*uaddr_len = sizeof(struct full_sockaddr_ax25);
if (sk->protinfo.ax25->digipeat != NULL) {
ndigi = sk->protinfo.ax25->digipeat->ndigi;
sax->fsa_ax25.sax25_ndigis = ndigi;
for (i = 0; i < ndigi; i++)
sax->fsa_digipeater[i] = sk->protinfo.ax25->digipeat->calls[i];
}
} else {
sax->fsa_ax25.sax25_family = AF_AX25;
sax->fsa_ax25.sax25_call = sk->protinfo.ax25->source_addr;
sax->fsa_ax25.sax25_ndigis = 1;
*uaddr_len = sizeof(struct full_sockaddr_ax25);
if (sk->protinfo.ax25->device != NULL)
memcpy(&sax->fsa_digipeater[0], sk->protinfo.ax25->device->dev_addr, AX25_ADDR_LEN);
else
sax->fsa_digipeater[0] = null_ax25_address;
}
return 0;
}
static int ax25_rcv(struct sk_buff *skb, struct device *dev, ax25_address *dev_addr, struct packet_type *ptype)
{
struct sock *make;
struct sock *sk;
int type = 0;
ax25_digi dp;
ax25_cb *ax25;
ax25_address src, dest;
struct sock *raw;
int mine = 0;
int dama;
/*
* Process the AX.25/LAPB frame.
*/
skb->h.raw = skb->data;
#ifdef CONFIG_FIREWALL
if (call_in_firewall(PF_AX25, skb->dev, skb->h.raw, NULL) != FW_ACCEPT) {
kfree_skb(skb, FREE_READ);
return 0;
}
#endif
/*
* Parse the address header.
*/
if (ax25_parse_addr(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL) {
kfree_skb(skb, FREE_READ);
return 0;
}
/*
* Ours perhaps ?
*/
if (dp.lastrepeat + 1 < dp.ndigi) { /* Not yet digipeated completely */
if (ax25cmp(&dp.calls[dp.lastrepeat + 1], dev_addr) == 0) {
struct device *dev_out = dev;
skb=skb_unshare(skb, GFP_ATOMIC, FREE_READ);
if(skb==NULL)
return 0;
/* We are the digipeater. Mark ourselves as repeated
and throw the packet back out of the same device */
dp.lastrepeat++;
dp.repeated[(int)dp.lastrepeat] = 1;
if (ax25_dev_get_value(dev, AX25_VALUES_DIGI) & AX25_DIGI_XBAND) {
while (dp.lastrepeat + 1 < dp.ndigi) {
struct device *dev_scan;
if ((dev_scan = ax25rtr_get_dev(&dp.calls[dp.lastrepeat + 1])) == NULL)
break;
dp.lastrepeat++;
dp.repeated[(int)dp.lastrepeat] = 1;
dev_out = dev_scan;
}
if (dev != dev_out && (ax25_dev_get_value(dev_out, AX25_VALUES_DIGI) & AX25_DIGI_XBAND) == 0) {
kfree_skb(skb, FREE_READ);
return 0;
}
}
if (dev == dev_out && (ax25_dev_get_value(dev, AX25_VALUES_DIGI) & AX25_DIGI_INBAND) == 0) {
kfree_skb(skb, FREE_READ);
return 0;
}
build_ax25_addr(skb->data, &src, &dest, &dp, type, MODULUS);
#ifdef CONFIG_FIREWALL
if (call_fw_firewall(PF_AX25, skb->dev, skb->data, NULL) != FW_ACCEPT) {
kfree_skb(skb, FREE_READ);
return 0;
}
#endif
skb->arp = 1;
ax25_queue_xmit(skb, dev_out, SOPRI_NORMAL);
} else {
kfree_skb(skb, FREE_READ);
}
return 0;
}
/*
* Pull of the AX.25 headers leaving the CTRL/PID bytes
*/
skb_pull(skb, size_ax25_addr(&dp));
/* For our port addresses ? */
if (ax25cmp(&dest, dev_addr) == 0)
mine = 1;
/* Also match on any registered callsign from L3/4 */
if (!mine && ax25_listen_mine(&dest, dev))
mine = 1;
if ((*skb->data & ~0x10) == LAPB_UI) { /* UI frame - bypass LAPB processing */
skb->h.raw = skb->data + 2; /* skip control and pid */
if ((raw = ax25_addr_match(&dest)) != NULL)
ax25_send_to_raw(raw, skb, skb->data[1]);
if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
kfree_skb(skb, FREE_READ);
return 0;
}
/* Now we are pointing at the pid byte */
switch (skb->data[1]) {
#ifdef CONFIG_INET
case AX25_P_IP:
skb_pull(skb,2); /* drop PID/CTRL */
ip_rcv(skb, dev, ptype); /* Note ptype here is the wrong one, fix me later */
break;
case AX25_P_ARP:
skb_pull(skb,2);
arp_rcv(skb, dev, ptype); /* Note ptype here is wrong... */
break;
#endif
case AX25_P_TEXT:
/* Now find a suitable dgram socket */
if ((sk = ax25_find_socket(&dest, &src, SOCK_DGRAM)) != NULL) {
if (sk->rmem_alloc >= sk->rcvbuf) {
kfree_skb(skb, FREE_READ);
} else {
/*
* Remove the control and PID.
*/
skb_pull(skb, 2);
skb_queue_tail(&sk->receive_queue, skb);
skb->sk = sk;
atomic_add(skb->truesize, &sk->rmem_alloc);
if (!sk->dead)
sk->data_ready(sk, skb->len);
}
} else {
kfree_skb(skb, FREE_READ);
}
break;
default:
kfree_skb(skb, FREE_READ); /* Will scan SOCK_AX25 RAW sockets */
break;
}
return 0;
}
/*
* Is connected mode supported on this device ?
* If not, should we DM the incoming frame (except DMs) or
* silently ignore them. For now we stay quiet.
*/
if (!ax25_dev_get_value(dev, AX25_VALUES_CONMODE)) {
kfree_skb(skb, FREE_READ);
return 0;
}
/* LAPB */
/* AX.25 state 1-4 */
if ((ax25 = ax25_find_cb(&dest, &src, dev)) != NULL) {
/*
* Process the frame. If it is queued up internally it returns one otherwise we
* free it immediately. This routine itself wakes the user context layers so we
* do no further work
*/
if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
kfree_skb(skb, FREE_READ);
return 0;
}
/* AX.25 state 0 (disconnected) */
/* a) received not a SABM(E) */
if ((*skb->data & ~PF) != SABM && (*skb->data & ~PF) != SABME) {
/*
* Never reply to a DM. Also ignore any connects for
* addresses that are not our interfaces and not a socket.
*/
if ((*skb->data & ~PF) != DM && mine)
ax25_return_dm(dev, &src, &dest, &dp);
kfree_skb(skb, FREE_READ);
return 0;
}
/* b) received SABM(E) */
if ((sk = ax25_find_listener(&dest, dev, SOCK_SEQPACKET)) != NULL) {
if (sk->ack_backlog == sk->max_ack_backlog || (make = ax25_make_new(sk, dev)) == NULL) {
if (mine)
ax25_return_dm(dev, &src, &dest, &dp);
kfree_skb(skb, FREE_READ);
return 0;
}
ax25 = make->protinfo.ax25;
skb_queue_head(&sk->receive_queue, skb);
skb->sk = make;
make->state = TCP_ESTABLISHED;
make->pair = sk;
sk->ack_backlog++;
} else {
if (!mine) {
kfree_skb(skb, FREE_READ);
return 0;
}
if ((ax25 = ax25_create_cb()) == NULL) {
ax25_return_dm(dev, &src, &dest, &dp);
kfree_skb(skb, FREE_READ);
return 0;
}
ax25_fillin_cb(ax25, dev);
ax25->idletimer = ax25->idle;
}
ax25->source_addr = dest;
ax25->dest_addr = src;
/*
* Sort out any digipeated paths.
*/
if (dp.ndigi != 0 && ax25->digipeat == NULL && (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
kfree_skb(skb, FREE_READ);
ax25_destroy_socket(ax25);
return 0;
}
if (dp.ndigi == 0) {
if (ax25->digipeat != NULL) {
kfree_s(ax25->digipeat, sizeof(ax25_digi));
ax25->digipeat = NULL;
}
} else {
/* Reverse the source SABM's path */
ax25_digi_invert(&dp, ax25->digipeat);
}
if ((*skb->data & ~PF) == SABME) {
ax25->modulus = EMODULUS;
ax25->window = ax25_dev_get_value(dev, AX25_VALUES_EWINDOW);
} else {
ax25->modulus = MODULUS;
ax25->window = ax25_dev_get_value(dev, AX25_VALUES_WINDOW);
}
ax25->device = dev;
ax25_send_control(ax25, UA, POLLON, C_RESPONSE);
if (dama) ax25_dama_on(ax25); /* bke 951121 */
ax25->dama_slave = dama;
ax25->t3timer = ax25->t3;
ax25->state = AX25_STATE_3;
ax25_insert_socket(ax25);
ax25_set_timer(ax25);
if (sk != NULL) {
if (!sk->dead)
sk->data_ready(sk, skb->len);
} else {
kfree_skb(skb, FREE_READ);
}
return 0;
}
/*
* Receive an AX.25 frame via a SLIP interface.
*/
static int kiss_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *ptype)
{
skb->sk = NULL; /* Initially we don't know who it's for */
if ((*skb->data & 0x0F) != 0) {
kfree_skb(skb, FREE_READ); /* Not a KISS data frame */
return 0;
}
skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
}
static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock, int flags)
{
struct sock *sk = (struct sock *)sock->data;
struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
int err;
struct sockaddr_ax25 sax;
struct sk_buff *skb;
unsigned char *asmptr;
int size;
ax25_digi *dp;
ax25_digi dtmp;
int lv;
int addr_len = msg->msg_namelen;
if (sk->err)
return sock_error(sk);
if (flags || msg->msg_control)
return -EINVAL;
if (sk->zapped)
return -EADDRNOTAVAIL;
if (sk->shutdown & SEND_SHUTDOWN) {
send_sig(SIGPIPE, current, 0);
return -EPIPE;
}
if (sk->protinfo.ax25->device == NULL)
return -ENETUNREACH;
if (usax) {
if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
return -EINVAL;
if (usax->sax25_family != AF_AX25)
return -EINVAL;
if (addr_len == sizeof(struct full_sockaddr_ax25) && usax->sax25_ndigis != 0) {
int ct = 0;
struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
/* Valid number of digipeaters ? */
if (usax->sax25_ndigis < 1 || usax->sax25_ndigis > AX25_MAX_DIGIS)
return -EINVAL;
dtmp.ndigi = usax->sax25_ndigis;
while (ct < usax->sax25_ndigis) {
dtmp.repeated[ct] = 0;
dtmp.calls[ct] = fsa->fsa_digipeater[ct];
ct++;
}
dtmp.lastrepeat = 0;
}
sax = *usax;
if (sk->type == SOCK_SEQPACKET && ax25cmp(&sk->protinfo.ax25->dest_addr, &sax.sax25_call) != 0)
return -EISCONN;
if (usax->sax25_ndigis == 0)
dp = NULL;
else
dp = &dtmp;
} else {
/*
* FIXME: 1003.1g - if the socket is like this because
* it has become closed (not started closed) and is VC
* we ought to SIGPIPE, EPIPE
*/
if (sk->state != TCP_ESTABLISHED)
return -ENOTCONN;
sax.sax25_family = AF_AX25;
sax.sax25_call = sk->protinfo.ax25->dest_addr;
dp = sk->protinfo.ax25->digipeat;
}
if (sk->debug)
printk("AX.25: sendto: Addresses built.\n");
/* Build a packet */
if (sk->debug)
printk("AX.25: sendto: building packet.\n");
/* Assume the worst case */
size = len + 3 + size_ax25_addr(dp) + AX25_BPQ_HEADER_LEN;
if ((skb = sock_alloc_send_skb(sk, size, 0, 0, &err)) == NULL)
return err;
skb->sk = sk;
skb->free = 1;
skb->arp = 1;
skb_reserve(skb, size - len);
if (sk->debug)
printk("AX.25: Appending user data\n");
/* User data follows immediately after the AX.25 data */
memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
/* Add the PID, usually AX25_TEXT */
asmptr = skb_push(skb, 1);
*asmptr = sk->protocol;
if (sk->debug)
printk("AX.25: Transmitting buffer\n");
if (sk->type == SOCK_SEQPACKET) {
/* Connected mode sockets go via the LAPB machine */
if (sk->state != TCP_ESTABLISHED) {
kfree_skb(skb, FREE_WRITE);
return -ENOTCONN;
}
ax25_output(sk->protinfo.ax25, skb); /* Shove it onto the queue and kick */
return len;
} else {
asmptr = skb_push(skb, 1 + size_ax25_addr(dp));
if (sk->debug) {
printk("Building AX.25 Header (dp=%p).\n", dp);
if (dp != 0)
printk("Num digipeaters=%d\n", dp->ndigi);
}
/* Build an AX.25 header */
asmptr += (lv = build_ax25_addr(asmptr, &sk->protinfo.ax25->source_addr, &sax.sax25_call, dp, C_COMMAND, MODULUS));
if (sk->debug)
printk("Built header (%d bytes)\n",lv);
skb->h.raw = asmptr;
if (sk->debug)
printk("base=%p pos=%p\n", skb->data, asmptr);
*asmptr = LAPB_UI;
/* Datagram frames go straight out of the door as UI */
ax25_queue_xmit(skb, sk->protinfo.ax25->device, SOPRI_NORMAL);
return len;
}
}
static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
{
struct sock *sk = (struct sock *)sock->data;
struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
int copied, length;
struct sk_buff *skb;
int er;
int dama;
if (addr_len != NULL)
*addr_len = sizeof(*sax);
/*
* This works for seqpacket too. The receiver has ordered the
* queue for us! We do one quick check first though
*/
if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
return -ENOTCONN;
/* Now we can treat all alike */
if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
return er;
if (sk->protinfo.ax25->hdrincl) {
length = skb->len + (skb->data - skb->h.raw);
} else {
if (sk->type == SOCK_SEQPACKET)
skb_pull(skb, 1); /* Remove PID */
length = skb->len;
skb->h.raw = skb->data;
}
copied = length;
if (copied > size) {
copied = size;
msg->msg_flags |= MSG_TRUNC;
}
skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
if (sax) {
ax25_digi digi;
ax25_address dest;
if (addr_len == (int *)0)
return -EINVAL;
if (*addr_len != sizeof(struct sockaddr_ax25) && *addr_len != sizeof(struct full_sockaddr_ax25))
return -EINVAL;
ax25_parse_addr(skb->data, skb->len, NULL, &dest, &digi, NULL, &dama);
sax->sax25_family = AF_AX25;
/* We set this correctly, even though we may not let the
application know the digi calls further down (because it
did NOT ask to know them). This could get political... **/
sax->sax25_ndigis = digi.ndigi;
sax->sax25_call = dest;
*addr_len = sizeof(struct sockaddr_ax25);
if (*addr_len == sizeof(struct full_sockaddr_ax25) && sax->sax25_ndigis != 0) {
int ct = 0;
struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax;
while (ct < digi.ndigi) {
fsa->fsa_digipeater[ct] = digi.calls[ct];
ct++;
}
*addr_len = sizeof(struct full_sockaddr_ax25);
}
}
skb_free_datagram(sk, skb);
return copied;
}
static int ax25_shutdown(struct socket *sk, int how)
{
/* FIXME - generate DM and RNR states */
return -EOPNOTSUPP;
}
static int ax25_select(struct socket *sock , int sel_type, select_table *wait)
{
struct sock *sk = (struct sock *)sock->data;
return datagram_select(sk, sel_type, wait);
}
static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
struct sock *sk = (struct sock *)sock->data;
int err;
long amount = 0;
switch (cmd) {
case TIOCOUTQ:
if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(int))) != 0)
return err;
amount = sk->sndbuf - sk->wmem_alloc;
if (amount < 0)
amount = 0;
put_user(amount, (int *)arg);
return 0;
case TIOCINQ: {
struct sk_buff *skb;
/* These two are safe on a single CPU system as only user tasks fiddle here */
if ((skb = skb_peek(&sk->receive_queue)) != NULL)
amount = skb->len;
if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(int))) != 0)
return err;
put_user(amount, (int *)arg);
return 0;
}
case SIOCGSTAMP:
if (sk != NULL) {
if (sk->stamp.tv_sec==0)
return -ENOENT;
if ((err = verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval))) != 0)
return err;
copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval));
return 0;
}
return -EINVAL;
case SIOCAX25ADDUID: /* Add a uid to the uid/call map table */
case SIOCAX25DELUID: /* Delete a uid from the uid/call map table */
case SIOCAX25GETUID: {
struct sockaddr_ax25 sax25;
if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct sockaddr_ax25))) != 0)
return err;
copy_from_user(&sax25, (void *)arg, sizeof(sax25));
return ax25_uid_ioctl(cmd, &sax25);
}
case SIOCAX25NOUID: /* Set the default policy (default/bar) */
if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(unsigned long))) != 0)
return err;
if (!suser())
return -EPERM;
get_user(amount, (long *)arg);
if (amount > AX25_NOUID_BLOCK)
return -EINVAL;
ax25_uid_policy = amount;
return 0;
case SIOCADDRT:
case SIOCDELRT:
case SIOCAX25OPTRT:
if (!suser())
return -EPERM;
return ax25_rt_ioctl(cmd, (void *)arg);
case SIOCAX25CTLCON:
if (!suser())
return -EPERM;
return ax25_ctl_ioctl(cmd, (void *)arg);
case SIOCGIFADDR:
case SIOCSIFADDR:
case SIOCGIFDSTADDR:
case SIOCSIFDSTADDR:
case SIOCGIFBRDADDR:
case SIOCSIFBRDADDR:
case SIOCGIFNETMASK:
case SIOCSIFNETMASK:
case SIOCGIFMETRIC:
case SIOCSIFMETRIC:
return -EINVAL;
default:
return dev_ioctl(cmd, (void *)arg);
}
/*NOTREACHED*/
return 0;
}
static int ax25_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
{
ax25_cb *ax25;
struct device *dev;
const char *devname;
int len = 0;
off_t pos = 0;
off_t begin = 0;
cli();
len += sprintf(buffer, "dest_addr src_addr dev st vs vr va t1 t2 t3 idle n2 rtt wnd paclen dama Snd-Q Rcv-Q\n");
for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
if ((dev = ax25->device) == NULL)
devname = "???";
else
devname = dev->name;
len += sprintf(buffer + len, "%-9s ",
ax2asc(&ax25->dest_addr));
len += sprintf(buffer + len, "%-9s %-4s %2d %3d %3d %3d %3d/%03d %2d/%02d %3d/%03d %3d/%03d %2d/%02d %3d %3d %5d",
ax2asc(&ax25->source_addr), devname,
ax25->state,
ax25->vs, ax25->vr, ax25->va,
ax25->t1timer / PR_SLOWHZ,
ax25->t1 / PR_SLOWHZ,
ax25->t2timer / PR_SLOWHZ,
ax25->t2 / PR_SLOWHZ,
ax25->t3timer / PR_SLOWHZ,
ax25->t3 / PR_SLOWHZ,
ax25->idletimer / (PR_SLOWHZ * 60),
ax25->idle / (PR_SLOWHZ * 60),
ax25->n2count, ax25->n2,
ax25->rtt / PR_SLOWHZ,
ax25->window,
ax25->paclen);
len += sprintf(buffer + len, " %s", ax25->dama_slave ? " slave" : " no");
if (ax25->sk != NULL) {
len += sprintf(buffer + len, " %5d %5d\n",
ax25->sk->wmem_alloc,
ax25->sk->rmem_alloc);
} else {
len += sprintf(buffer + len, "\n");
}
pos = begin + len;
if (pos < offset) {
len = 0;
begin = pos;
}
if (pos > offset + length)
break;
}
sti();
*start = buffer + (offset - begin);
len -= (offset - begin);
if (len > length) len = length;
return(len);
}
static struct proto_ops ax25_proto_ops = {
AF_AX25,
ax25_create,
ax25_dup,
ax25_release,
ax25_bind,
ax25_connect,
ax25_socketpair,
ax25_accept,
ax25_getname,
ax25_select,
ax25_ioctl,
ax25_listen,
ax25_shutdown,
ax25_setsockopt,
ax25_getsockopt,
ax25_fcntl,
ax25_sendmsg,
ax25_recvmsg
};
/*
* Called by socket.c on kernel start up
*/
static struct packet_type ax25_packet_type =
{
0, /* MUTTER ntohs(ETH_P_AX25),*/
0, /* copy */
kiss_rcv,
NULL,
NULL,
};
static struct notifier_block ax25_dev_notifier = {
ax25_device_event,
0
};
static struct symbol_table ax25_syms = {
#include <linux/symtab_begin.h>
X(ax25_encapsulate),
X(ax25_rebuild_header),
#if defined(CONFIG_NETROM_MODULE) || defined(CONFIG_ROSE_MODULE)
X(ax25_findbyuid),
X(ax25_link_up),
X(ax25_linkfail_register),
X(ax25_linkfail_release),
X(ax25_listen_register),
X(ax25_listen_release),
X(ax25_protocol_register),
X(ax25_protocol_release),
X(ax25_send_frame),
X(ax25_uid_policy),
X(ax25cmp),
X(ax2asc),
X(asc2ax),
X(null_ax25_address),
#endif
#include <linux/symtab_end.h>
};
#ifdef CONFIG_PROC_FS
static struct proc_dir_entry proc_ax25_route = {
PROC_NET_AX25_ROUTE, 10, "ax25_route",
S_IFREG | S_IRUGO, 1, 0, 0,
0, &proc_net_inode_operations,
ax25_rt_get_info
};
static struct proc_dir_entry proc_ax25 = {
PROC_NET_AX25, 4, "ax25",
S_IFREG | S_IRUGO, 1, 0, 0,
0, &proc_net_inode_operations,
ax25_get_info
};
static struct proc_dir_entry proc_ax25_calls = {
PROC_NET_AX25_CALLS, 10, "ax25_calls",
S_IFREG | S_IRUGO, 1, 0, 0,
0, &proc_net_inode_operations,
ax25_cs_get_info
};
#endif
void ax25_proto_init(struct net_proto *pro)
{
sock_register(ax25_proto_ops.family, &ax25_proto_ops);
ax25_packet_type.type = htons(ETH_P_AX25);
dev_add_pack(&ax25_packet_type);
register_netdevice_notifier(&ax25_dev_notifier);
register_symtab(&ax25_syms);
ax25_register_sysctl();
#ifdef CONFIG_PROC_FS
proc_net_register(&proc_ax25_route);
proc_net_register(&proc_ax25);
proc_net_register(&proc_ax25_calls);
#endif
printk(KERN_INFO "G4KLX/GW4PTS AX.25 for Linux. Version 0.34 for Linux NET3.037 (Linux 2.1)\n");
}
/*
* A small shim to dev_queue_xmit to add the KISS control byte.
*/
void ax25_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
{
unsigned char *ptr;
#ifdef CONFIG_FIREWALL
if (call_out_firewall(PF_AX25, skb->dev, skb->data, NULL) != FW_ACCEPT) {
dev_kfree_skb(skb, FREE_WRITE);
return;
}
#endif
skb->protocol = htons(ETH_P_AX25);
ptr = skb_push(skb, 1);
*ptr++ = 0; /* KISS */
dev_queue_xmit(skb, dev, pri);
}
/*
* IP over AX.25 encapsulation.
*/
/*
* Shove an AX.25 UI header on an IP packet and handle ARP
*/
#ifdef CONFIG_INET
int ax25_encapsulate(struct sk_buff *skb, struct device *dev, unsigned short type, void *daddr,
void *saddr, unsigned len)
{
/* header is an AX.25 UI frame from us to them */
unsigned char *buff = skb_push(skb, AX25_HEADER_LEN);
*buff++ = 0; /* KISS DATA */
if (daddr != NULL)
memcpy(buff, daddr, dev->addr_len); /* Address specified */
buff[6] &= ~LAPB_C;
buff[6] &= ~LAPB_E;
buff[6] |= SSSID_SPARE;
buff += AX25_ADDR_LEN;
if (saddr != NULL)
memcpy(buff, saddr, dev->addr_len);
else
memcpy(buff, dev->dev_addr, dev->addr_len);
buff[6] &= ~LAPB_C;
buff[6] |= LAPB_E;
buff[6] |= SSSID_SPARE;
buff += AX25_ADDR_LEN;
*buff++ = LAPB_UI; /* UI */
/* Append a suitable AX.25 PID */
switch (type) {
case ETH_P_IP:
*buff++ = AX25_P_IP;
break;
case ETH_P_ARP:
*buff++ = AX25_P_ARP;
break;
default:
printk(KERN_ERR "AX.25 wrong protocol type 0x%x2.2\n", type);
*buff++ = 0;
break;
}
if (daddr != NULL)
return AX25_HEADER_LEN;
return -AX25_HEADER_LEN; /* Unfinished header */
}
int ax25_rebuild_header(unsigned char *bp, struct device *dev, unsigned long dest, struct sk_buff *skb)
{
struct sk_buff *ourskb;
int mode;
if (arp_find(bp + 1, dest, dev, dev->pa_addr, skb))
return 1;
if (bp[16] == AX25_P_IP) {
mode = ax25_ip_mode_get((ax25_address *)(bp + 1), dev);
if (mode == 'V' || (mode == ' ' && ax25_dev_get_value(dev, AX25_VALUES_IPDEFMODE))) {
/*
* This is a workaround to try to keep the device locking
* straight until skb->free=0 is abolished post 1.4.
*
* We clone the buffer and release the original thereby
* keeping it straight
*
* Note: we report 1 back so the caller will
* not feed the frame direct to the physical device
* We don't want that to happen. (It won't be upset
* as we have pulled the frame from the queue by
* freeing it).
*/
if ((ourskb = skb_clone(skb, GFP_ATOMIC)) == NULL) {
dev_kfree_skb(skb, FREE_WRITE);
return 1;
}
ourskb->sk = skb->sk;
if (ourskb->sk != NULL)
atomic_add(ourskb->truesize, &ourskb->sk->wmem_alloc);
dev_kfree_skb(skb, FREE_WRITE);
skb_pull(ourskb, AX25_HEADER_LEN - 1); /* Keep PID */
ax25_send_frame(ourskb, (ax25_address *)(bp + 8), (ax25_address *)(bp + 1), NULL, dev);
return 1;
}
}
bp[7] &= ~LAPB_C;
bp[7] &= ~LAPB_E;
bp[7] |= SSSID_SPARE;
bp[14] &= ~LAPB_C;
bp[14] |= LAPB_E;
bp[14] |= SSSID_SPARE;
/*
* dl1bke 960317: we use ax25_queue_xmit here to allow mode datagram
* over ethernet. I don't know if this is valid, though.
*/
ax25_dg_build_path(skb, (ax25_address *)(bp + 1), dev);
ax25_queue_xmit(skb, dev, SOPRI_NORMAL);
return 1;
}
#endif
#ifdef MODULE
int init_module(void)
{
ax25_proto_init(NULL);
return 0;
}
void cleanup_module(void)
{
#ifdef CONFIG_PROC_FS
proc_net_unregister(PROC_NET_AX25_ROUTE);
proc_net_unregister(PROC_NET_AX25);
proc_net_unregister(PROC_NET_AX25_CALLS);
proc_net_unregister(PROC_NET_AX25_ROUTE);
#endif
ax25_rt_free();
ax25_unregister_sysctl();
unregister_netdevice_notifier(&ax25_dev_notifier);
ax25_packet_type.type = htons(ETH_P_AX25);
dev_remove_pack(&ax25_packet_type);
sock_unregister(ax25_proto_ops.family);
}
#endif
#endif
|