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
|
/* Copyright 2007-2010 Jozsef Kadlecsik (kadlec@netfilter.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <assert.h> /* assert */
#include <endian.h> /* htobe64 */
#include <errno.h> /* errno */
#include <setjmp.h> /* setjmp, longjmp */
#include <stdio.h> /* snprintf */
#include <stdarg.h> /* va_* */
#include <stdbool.h> /* bool */
#include <stdlib.h> /* free */
#include <string.h> /* str* */
#include <unistd.h> /* getpagesize */
#include <net/ethernet.h> /* ETH_ALEN */
#include <net/if.h> /* IFNAMSIZ */
#include <libipset/compat.h> /* be64toh() */
#include <libipset/debug.h> /* D() */
#include <libipset/data.h> /* IPSET_OPT_* */
#include <libipset/errcode.h> /* ipset_errcode */
#include <libipset/print.h> /* ipset_print_* */
#include <libipset/types.h> /* struct ipset_type */
#include <libipset/transport.h> /* transport */
#include <libipset/mnl.h> /* default backend */
#include <libipset/utils.h> /* STREQ */
#include <libipset/ipset.h> /* IPSET_ENV_* */
#include <libipset/list_sort.h> /* list_sort */
#include <libipset/session.h> /* prototypes */
#define IPSET_NEST_MAX 4
/* When we want to sort the entries */
struct ipset_sorted {
struct list_head list;
size_t offset; /* Offset in outbuf */
};
/* The session structure */
struct ipset_session {
const struct ipset_transport *transport;/* Transport protocol */
struct ipset_handle *handle; /* Transport handler */
struct ipset_data *data; /* Input/output data */
/* Command state */
enum ipset_cmd cmd; /* Current command */
uint32_t lineno; /* Current lineno in restore mode */
uint32_t printed_set; /* Printed sets so far */
char saved_setname[IPSET_MAXNAMELEN]; /* Saved setname */
const struct ipset_type *saved_type; /* Saved type */
struct nlattr *nested[IPSET_NEST_MAX]; /* Pointer to nest levels */
uint8_t nestid; /* Current nest level */
uint8_t protocol; /* The protocol used */
bool version_checked; /* Version checked */
/* Output buffer */
char *outbuf; /* Output buffer */
size_t outbuflen; /* Output buffer size */
size_t pos; /* Printing position in outbuf */
struct list_head sorted; /* Sorted entries */
struct list_head pool; /* Pool to reuse */
enum ipset_output_mode mode; /* Output mode */
ipset_print_outfn print_outfn; /* Output function to file */
void *p; /* Private data for print_outfn */
bool sort; /* Print sorted hash:* types */
/* Session IO */
bool normal_io, full_io; /* Default/normal/full IO */
FILE *istream, *ostream; /* Session input/output stream */
/* Error/warning reporting */
char report[IPSET_ERRORBUFLEN]; /* Error/report buffer */
enum ipset_err_type err_type; /* ERROR/WARNING/NOTICE */
uint8_t envopts; /* Session env opts */
/* Kernel message buffer */
size_t bufsize;
void *buffer;
};
/*
* Glue functions
*/
/**
* ipset_session_data - return pointer to the data
* @session: session structure
*
* Returns the pointer to the data structure of the session.
*/
struct ipset_data *
ipset_session_data(const struct ipset_session *session)
{
assert(session);
return session->data;
}
/**
* ipset_session_handle - return pointer to the handle
* @session: session structure
*
* Returns the pointer to the transport handle structure of the session.
*/
struct ipset_handle *
ipset_session_handle(const struct ipset_session *session)
{
assert(session);
return session->handle;
}
/**
* ipset_saved_type - return pointer to the saved type
* @session: session structure
*
* Returns the pointer to the saved type from the last ipset_cmd
* It is required to decode type-specific error codes in restore mode.
*/
const struct ipset_type *
ipset_saved_type(const struct ipset_session *session)
{
assert(session);
return session->saved_type;
}
/**
* ipset_session_lineno - set session lineno
* @session: session structure
*
* Set session lineno to report parser errors correctly.
*/
void
ipset_session_lineno(struct ipset_session *session, uint32_t lineno)
{
assert(session);
session->lineno = lineno;
}
/**
* ipset_session_printf_private - returns the session private pointer
* @session: session structure
*
* Returns the private pointer in the session structure,
* for private/custom print fuctions.
*/
void *
ipset_session_printf_private(struct ipset_session *session)
{
assert(session);
return session->p;
}
/*
* Environment options
*/
/**
* ipset_envopt_test - test environment option
* @session: session structure
* @opt: environment option
*
* Test whether the environment option is set in the session.
*
* Returns true or false.
*/
bool
ipset_envopt_test(struct ipset_session *session, enum ipset_envopt opt)
{
assert(session);
return session->envopts & opt;
}
/**
* ipset_envopt_set - set environment option
* @session: session structure
* @opt: environment option
*
* Set an environment option of the session.
*/
void
ipset_envopt_set(struct ipset_session *session, enum ipset_envopt opt)
{
assert(session);
session->envopts |= opt;
}
/**
* ipset_envopt_unset - unset environment option
* @session: session structure
* @opt: environment option
*
* Unset an environment option of the session.
*/
void
ipset_envopt_unset(struct ipset_session *session, enum ipset_envopt opt)
{
assert(session);
session->envopts &= ~opt;
}
/**
* ipset_session_output - set the session output mode
* @session: session structure
* @mode: output mode
*
* Set the output mode for the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_session_output(struct ipset_session *session,
enum ipset_output_mode mode)
{
assert(session);
session->mode = mode;
return 0;
}
/*
* Error and warning reporting
*/
/**
* ipset_session_report - fill the report buffer
* @session: session structure
* @type: report type
* @fmt: message format
*
* Fill the report buffer with an error or warning message.
* Depending on the report type, set the error or warning
* message pointer.
*
* Returns -1.
*/
int __attribute__((format(printf, 3, 4)))
ipset_session_report(struct ipset_session *session,
enum ipset_err_type type,
const char *fmt, ...)
{
int len, offset = 0;
va_list args;
assert(session);
assert(fmt);
/* Suppress warning/notice when more important message is required */
if (session->err_type > IPSET_NO_ERROR && session->err_type < type)
session->report[0] = '\0';
if (session->lineno != 0 && type == IPSET_ERROR) {
sprintf(session->report, "Error in line %u: ",
session->lineno);
}
offset = strlen(session->report);
va_start(args, fmt);
len = vsnprintf(session->report + offset,
IPSET_ERRORBUFLEN - 1 - offset,
fmt, args);
va_end(args);
if (len >= IPSET_ERRORBUFLEN - 1 - offset)
session->report[IPSET_ERRORBUFLEN - 1] = '\0';
if (strlen(session->report) < IPSET_ERRORBUFLEN - 1)
strcat(session->report, "\n");
session->err_type = type;
if (type == IPSET_ERROR)
ipset_data_reset(ipset_session_data(session));
return -1;
}
/**
* ipset_session_warning_as_error - set warning as error
* @session: session structrure
*
* Returns -1.
*/
int
ipset_session_warning_as_error(struct ipset_session *session)
{
session->err_type = IPSET_ERROR;
ipset_data_reset(ipset_session_data(session));
return -1;
}
/**
* ipset_session_reset - reset the report buffer
* @session: session structure
*
* Reset the report buffer, the error and warning pointers.
*/
void
ipset_session_report_reset(struct ipset_session *session)
{
assert(session);
session->report[0] = '\0';
session->err_type = IPSET_NO_ERROR;
}
/**
* ipset_session_report_msg - return the report buffer
* @session: session structure
*
* Return the pointer to the report buffer.
* If there is no error message, the buffer is empty.
*/
const char *
ipset_session_report_msg(const struct ipset_session *session)
{
assert(session);
return session->report;
}
/**
* ipset_session_report_type - return the type of the report
* @session: session structure
*
* Return the type of the message in the report buffer.
*/
enum ipset_err_type
ipset_session_report_type(const struct ipset_session *session)
{
assert(session);
return session->err_type;
}
/*
* Receive data from the kernel
*/
struct ipset_attr_policy {
uint16_t type;
uint16_t len;
enum ipset_opt opt;
};
/* Attribute policies and mapping to options */
static const struct ipset_attr_policy cmd_attrs[] = {
[IPSET_ATTR_PROTOCOL] = {
.type = MNL_TYPE_U8,
},
[IPSET_ATTR_SETNAME] = {
.type = MNL_TYPE_NUL_STRING,
.opt = IPSET_SETNAME,
.len = IPSET_MAXNAMELEN,
},
[IPSET_ATTR_TYPENAME] = {
.type = MNL_TYPE_NUL_STRING,
.opt = IPSET_OPT_TYPENAME,
.len = IPSET_MAXNAMELEN,
},
/* IPSET_ATTR_SETNAME2 is an alias for IPSET_ATTR_TYPENAME */
[IPSET_ATTR_REVISION] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_REVISION,
},
[IPSET_ATTR_FAMILY] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_FAMILY,
},
[IPSET_ATTR_FLAGS] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_FLAGS,
},
[IPSET_ATTR_DATA] = {
.type = MNL_TYPE_NESTED,
},
[IPSET_ATTR_ADT] = {
.type = MNL_TYPE_NESTED,
},
[IPSET_ATTR_REVISION_MIN] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_REVISION_MIN,
},
/* IPSET_ATTR_PROTOCOL_MIN is an alias for IPSET_ATTR_REVISION_MIN */
[IPSET_ATTR_LINENO] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_LINENO,
},
[IPSET_ATTR_INDEX] = {
.type = MNL_TYPE_U16,
.opt = IPSET_OPT_INDEX,
},
};
static const struct ipset_attr_policy create_attrs[] = {
[IPSET_ATTR_IP] = {
.type = MNL_TYPE_NESTED,
.opt = IPSET_OPT_IP,
},
[IPSET_ATTR_IP_TO] = {
.type = MNL_TYPE_NESTED,
.opt = IPSET_OPT_IP_TO,
},
[IPSET_ATTR_CIDR] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_CIDR,
},
[IPSET_ATTR_PORT] = {
.type = MNL_TYPE_U16,
.opt = IPSET_OPT_PORT,
},
[IPSET_ATTR_PORT_TO] = {
.type = MNL_TYPE_U16,
.opt = IPSET_OPT_PORT_TO,
},
[IPSET_ATTR_TIMEOUT] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_TIMEOUT,
},
[IPSET_ATTR_PROTO] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_PROTO,
},
[IPSET_ATTR_CADT_FLAGS] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_CADT_FLAGS,
},
[IPSET_ATTR_GC] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_GC,
},
[IPSET_ATTR_HASHSIZE] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_HASHSIZE,
},
[IPSET_ATTR_MAXELEM] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_MAXELEM,
},
[IPSET_ATTR_MARKMASK] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_MARKMASK,
},
[IPSET_ATTR_NETMASK] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_NETMASK,
},
[IPSET_ATTR_PROBES] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_PROBES,
},
[IPSET_ATTR_RESIZE] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_RESIZE,
},
[IPSET_ATTR_SIZE] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_SIZE,
},
[IPSET_ATTR_ELEMENTS] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_ELEMENTS,
},
[IPSET_ATTR_REFERENCES] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_REFERENCES,
},
[IPSET_ATTR_MEMSIZE] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_MEMSIZE,
},
};
static const struct ipset_attr_policy adt_attrs[] = {
[IPSET_ATTR_IP] = {
.type = MNL_TYPE_NESTED,
.opt = IPSET_OPT_IP,
},
[IPSET_ATTR_IP_TO] = {
.type = MNL_TYPE_NESTED,
.opt = IPSET_OPT_IP_TO,
},
[IPSET_ATTR_CIDR] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_CIDR,
},
[IPSET_ATTR_MARK] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_MARK,
},
[IPSET_ATTR_PORT] = {
.type = MNL_TYPE_U16,
.opt = IPSET_OPT_PORT,
},
[IPSET_ATTR_PORT_TO] = {
.type = MNL_TYPE_U16,
.opt = IPSET_OPT_PORT_TO,
},
[IPSET_ATTR_PROTO] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_PROTO,
},
[IPSET_ATTR_TIMEOUT] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_TIMEOUT,
},
[IPSET_ATTR_CADT_FLAGS] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_CADT_FLAGS,
},
[IPSET_ATTR_LINENO] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_LINENO,
},
[IPSET_ATTR_ETHER] = {
.type = MNL_TYPE_BINARY,
.opt = IPSET_OPT_ETHER,
.len = ETH_ALEN,
},
[IPSET_ATTR_NAME] = {
.type = MNL_TYPE_NUL_STRING,
.opt = IPSET_OPT_NAME,
.len = IPSET_MAXNAMELEN,
},
[IPSET_ATTR_NAMEREF] = {
.type = MNL_TYPE_NUL_STRING,
.opt = IPSET_OPT_NAMEREF,
.len = IPSET_MAXNAMELEN,
},
[IPSET_ATTR_IP2] = {
.type = MNL_TYPE_NESTED,
.opt = IPSET_OPT_IP2,
},
[IPSET_ATTR_CIDR2] = {
.type = MNL_TYPE_U8,
.opt = IPSET_OPT_CIDR2,
},
[IPSET_ATTR_IP2_TO] = {
.type = MNL_TYPE_NESTED,
.opt = IPSET_OPT_IP2_TO,
},
[IPSET_ATTR_IFACE] = {
.type = MNL_TYPE_NUL_STRING,
.opt = IPSET_OPT_IFACE,
.len = IFNAMSIZ,
},
[IPSET_ATTR_PACKETS] = {
.type = MNL_TYPE_U64,
.opt = IPSET_OPT_PACKETS,
},
[IPSET_ATTR_BYTES] = {
.type = MNL_TYPE_U64,
.opt = IPSET_OPT_BYTES,
},
[IPSET_ATTR_COMMENT] = {
.type = MNL_TYPE_NUL_STRING,
.opt = IPSET_OPT_ADT_COMMENT,
.len = IPSET_MAX_COMMENT_SIZE + 1,
},
[IPSET_ATTR_SKBMARK] = {
.type = MNL_TYPE_U64,
.opt = IPSET_OPT_SKBMARK,
},
[IPSET_ATTR_SKBPRIO] = {
.type = MNL_TYPE_U32,
.opt = IPSET_OPT_SKBPRIO,
},
[IPSET_ATTR_SKBQUEUE] = {
.type = MNL_TYPE_U16,
.opt = IPSET_OPT_SKBQUEUE,
},
[IPSET_ATTR_PAD] = {
.type = MNL_TYPE_UNSPEC,
.len = 0,
},
};
static const struct ipset_attr_policy ipaddr_attrs[] = {
[IPSET_ATTR_IPADDR_IPV4] = {
.type = MNL_TYPE_U32,
},
[IPSET_ATTR_IPADDR_IPV6] = {
.type = MNL_TYPE_BINARY,
.len = sizeof(union nf_inet_addr),
},
};
#ifdef IPSET_DEBUG
static int debug = 1;
#endif
static int
generic_data_attr_cb(const struct nlattr *attr, void *data,
int attr_max, const struct ipset_attr_policy *policy)
{
const struct nlattr **tb = data;
int type = mnl_attr_get_type(attr);
IF_D(debug, "attr type: %u, len %u", type, attr->nla_len);
if (mnl_attr_type_valid(attr, attr_max) < 0) {
IF_D(debug, "attr type: %u INVALID", type);
return MNL_CB_ERROR;
}
if (mnl_attr_validate(attr, policy[type].type) < 0) {
IF_D(debug, "attr type: %u POLICY, attrlen %u", type,
mnl_attr_get_payload_len(attr));
return MNL_CB_ERROR;
}
if (policy[type].type == MNL_TYPE_NUL_STRING &&
mnl_attr_get_payload_len(attr) > policy[type].len)
return MNL_CB_ERROR;
tb[type] = attr;
return MNL_CB_OK;
}
static int
create_attr_cb(const struct nlattr *attr, void *data)
{
return generic_data_attr_cb(attr, data,
IPSET_ATTR_CREATE_MAX, create_attrs);
}
static int
adt_attr_cb(const struct nlattr *attr, void *data)
{
return generic_data_attr_cb(attr, data,
IPSET_ATTR_ADT_MAX, adt_attrs);
}
static int
ipaddr_attr_cb(const struct nlattr *attr, void *data)
{
return generic_data_attr_cb(attr, data,
IPSET_ATTR_IPADDR_MAX, ipaddr_attrs);
}
#define FAILURE(format, args...) \
{ ipset_err(session, format , ## args); return MNL_CB_ERROR; }
static int
attr2data(struct ipset_session *session, struct nlattr *nla[],
int type, const struct ipset_attr_policy attrs[])
{
struct ipset_data *data = session->data;
const struct ipset_attr_policy *attr;
const void *d;
uint64_t v64;
uint32_t v32;
uint16_t v16;
int ret;
attr = &attrs[type];
d = mnl_attr_get_payload(nla[type]);
if (attr->type == MNL_TYPE_UNSPEC)
return 0;
if (attr->type == MNL_TYPE_NESTED && attr->opt) {
/* IP addresses */
struct nlattr *ipattr[IPSET_ATTR_IPADDR_MAX+1] = {};
uint8_t family = ipset_data_family(data);
int atype;
D("IP attr type %u", type);
if (mnl_attr_parse_nested(nla[type],
ipaddr_attr_cb, ipattr) < 0)
FAILURE("Broken kernel message, cannot validate "
"IP address attribute!");
/* Validate by hand */
switch (family) {
case NFPROTO_IPV4:
atype = IPSET_ATTR_IPADDR_IPV4;
if (!ipattr[atype])
FAILURE("Broken kernel message: IPv4 address "
"expected but not received!");
if (ipattr[atype]->nla_len < sizeof(uint32_t))
FAILURE("Broken kernel message: "
"cannot validate IPv4 "
"address attribute!");
break;
case NFPROTO_IPV6:
atype = IPSET_ATTR_IPADDR_IPV6;
if (!ipattr[atype])
FAILURE("Broken kernel message: IPv6 address "
"expected but not received!");
if (ipattr[atype]->nla_len < sizeof(struct in6_addr))
FAILURE("Broken kernel message: "
"cannot validate IPv6 "
"address attribute!");
break;
default:
FAILURE("Broken kernel message: "
"IP address attribute but "
"family is unspecified!");
}
d = mnl_attr_get_payload(ipattr[atype]);
} else if (nla[type]->nla_type & NLA_F_NET_BYTEORDER) {
D("netorder attr type %u", type);
switch (attr->type) {
case MNL_TYPE_U64: {
uint64_t tmp;
/* Ensure data alignment */
memcpy(&tmp, d, sizeof(tmp));
v64 = be64toh(tmp);
d = &v64;
break;
}
case MNL_TYPE_U32: {
v32 = ntohl(*(const uint32_t *)d);
d = &v32;
break;
}
case MNL_TYPE_U16: {
v16 = ntohs(*(const uint16_t *)d);
d = &v16;
break;
}
default:
break;
}
} else if (attr->type == MNL_TYPE_NUL_STRING) {
if (!d || strlen(d) >= attr->len)
FAILURE("Broken kernel message: "
"string type attribute missing or too long!");
}
#ifdef IPSET_DEBUG
else
D("hostorder attr type %u", type);
if (type == IPSET_ATTR_TYPENAME)
D("nla typename %s", (const char *) d);
#endif
ret = ipset_data_set(data, attr->opt, d);
#ifdef IPSET_DEBUG
if (type == IPSET_ATTR_TYPENAME)
D("nla typename %s",
(const char *) ipset_data_get(data, IPSET_OPT_TYPENAME));
#endif
return ret;
}
#define ATTR2DATA(session, nla, type, attrs) \
if (attr2data(session, nla, type, attrs) < 0) \
return MNL_CB_ERROR
static const char cmd2name[][9] = {
[IPSET_CMD_NONE] = "NONE",
[IPSET_CMD_CREATE] = "CREATE",
[IPSET_CMD_DESTROY] = "DESTROY",
[IPSET_CMD_FLUSH] = "FLUSH",
[IPSET_CMD_RENAME] = "RENAME",
[IPSET_CMD_SWAP] = "SWAP",
[IPSET_CMD_LIST] = "LIST",
[IPSET_CMD_SAVE] = "SAVE",
[IPSET_CMD_ADD] = "ADD",
[IPSET_CMD_DEL] = "DEL",
[IPSET_CMD_TEST] = "TEST",
[IPSET_CMD_HEADER] = "HEADER",
[IPSET_CMD_TYPE] = "TYPE",
[IPSET_CMD_PROTOCOL] = "PROTOCOL",
};
static inline int
call_outfn(struct ipset_session *session)
{
int ret = session->print_outfn(session, session->p,
"%s", session->outbuf);
session->outbuf[0] = '\0';
session->pos = 0;
return ret < 0 ? ret : 0;
}
/* Handle printing failures */
static jmp_buf printf_failure;
static void
realloc_outbuf(struct ipset_session *session)
{
char *buf = realloc(session->outbuf,
session->outbuflen + IPSET_OUTBUFLEN);
if (!buf) {
ipset_err(session,
"Could not allocate memory to print sorted!");
longjmp(printf_failure, 1);
}
session->outbuf = buf;
session->outbuflen += IPSET_OUTBUFLEN;
}
static int
handle_snprintf_error(struct ipset_session *session,
int ret, int loop)
{
if (ret < 0 || ret + session->pos >= session->outbuflen) {
if (session->sort && !loop) {
realloc_outbuf(session);
return 1;
}
/* Buffer was too small, push it out and retry */
D("print buffer and try again: outbuflen: %lu, pos %lu, ret: %d",
session->outbuflen, session->pos, ret);
if (loop) {
ipset_err(session,
"Internal error at printing, loop detected!");
longjmp(printf_failure, 1);
}
session->outbuf[session->pos] = '\0';
if (call_outfn(session)) {
ipset_err(session,
"Internal error, could not print output buffer!");
longjmp(printf_failure, 1);
}
return 1;
}
session->pos += ret;
return 0;
}
static int __attribute__((format(printf, 2, 3)))
safe_snprintf(struct ipset_session *session, const char *fmt, ...)
{
va_list args;
int ret, loop = 0;
do {
D("outbuflen: %lu, pos: %lu, retry %u",
session->outbuflen, session->pos, loop);
va_start(args, fmt);
ret = vsnprintf(session->outbuf + session->pos,
session->outbuflen - session->pos,
fmt, args);
va_end(args);
loop = handle_snprintf_error(session, ret, loop);
} while (loop);
return ret;
}
static int
safe_dprintf(struct ipset_session *session, ipset_printfn fn,
enum ipset_opt opt)
{
int ret, loop = 0;
do {
D("outbuflen: %lu, len: %lu, retry %u",
session->outbuflen, session->pos, loop);
ret = fn(session->outbuf + session->pos,
session->outbuflen - session->pos,
session->data, opt, session->envopts);
loop = handle_snprintf_error(session, ret, loop);
} while (loop);
return ret;
}
static int
list_adt(struct ipset_session *session, struct nlattr *nla[])
{
const struct ipset_data *data = session->data;
const struct ipset_type *type;
const struct ipset_arg *arg;
size_t offset = 0;
int i, found = 0;
D("enter");
/* Check and load type, family */
if (!ipset_data_test(data, IPSET_OPT_TYPE))
type = ipset_type_get(session, IPSET_CMD_ADD);
else
type = ipset_data_get(data, IPSET_OPT_TYPE);
if (type == NULL)
return MNL_CB_ERROR;
for (i = IPSET_ATTR_UNSPEC + 1; i <= IPSET_ATTR_ADT_MAX; i++)
if (nla[i]) {
found++;
ATTR2DATA(session, nla, i, adt_attrs);
}
D("attr found %u", found);
if (!found)
return MNL_CB_OK;
if (session->sort) {
if (session->outbuflen <= session->pos + 1)
realloc_outbuf(session);
session->pos++; /* \0 */
offset = session->pos;
}
switch (session->mode) {
case IPSET_LIST_SAVE:
safe_snprintf(session, "add %s ", ipset_data_setname(data));
break;
case IPSET_LIST_XML:
safe_snprintf(session, "<member><elem>");
break;
case IPSET_LIST_PLAIN:
default:
break;
}
safe_dprintf(session, ipset_print_elem, IPSET_OPT_ELEM);
if (session->mode == IPSET_LIST_XML)
safe_snprintf(session, "</elem>");
for (i = 0; type->cmd[IPSET_ADD].args[i] != IPSET_ARG_NONE; i++) {
arg = ipset_keyword(type->cmd[IPSET_ADD].args[i]);
D("print arg opt %u (%s) %s", arg->opt, arg->name[0],
ipset_data_test(data, arg->opt) ? "(yes)" : "(missing)");
if (!(arg->print && ipset_data_test(data, arg->opt)))
continue;
switch (session->mode) {
case IPSET_LIST_SAVE:
case IPSET_LIST_PLAIN:
if (arg->has_arg == IPSET_NO_ARG) {
safe_snprintf(session, " %s", arg->name[0]);
break;
}
safe_snprintf(session, " %s ", arg->name[0]);
safe_dprintf(session, arg->print, arg->opt);
break;
case IPSET_LIST_XML:
if (arg->has_arg == IPSET_NO_ARG) {
safe_snprintf(session,
"<%s/>", arg->name[0]);
break;
}
safe_snprintf(session, "<%s>", arg->name[0]);
safe_dprintf(session, arg->print, arg->opt);
safe_snprintf(session, "</%s>", arg->name[0]);
break;
default:
break;
}
}
if (session->mode == IPSET_LIST_XML)
safe_snprintf(session, "</member>\n");
else
safe_snprintf(session, "\n");
if (session->sort) {
struct ipset_sorted *sorted;
if (!list_empty(&session->pool)) {
sorted = list_first_entry(&session->pool,
struct ipset_sorted, list);
list_del(&sorted->list);
} else {
sorted = calloc(1, sizeof(struct ipset_sorted));
if (!sorted) {
ipset_err(session,
"Could not allocate memory to print sorted!");
longjmp(printf_failure, 1);
}
}
sorted->offset = offset;
list_add_tail(&sorted->list, &session->sorted);
}
return MNL_CB_OK;
}
#define FAMILY_TO_STR(f) \
((f) == NFPROTO_IPV4 ? "inet" : \
(f) == NFPROTO_IPV6 ? "inet6" : "any")
static int
list_create(struct ipset_session *session, struct nlattr *nla[])
{
const struct ipset_data *data = session->data;
const struct ipset_type *type;
const struct ipset_arg *arg;
uint8_t family;
int i;
for (i = IPSET_ATTR_UNSPEC + 1; i <= IPSET_ATTR_CREATE_MAX; i++)
if (nla[i]) {
D("add attr %u, opt %u", i, create_attrs[i].opt);
ATTR2DATA(session, nla, i, create_attrs);
}
type = ipset_type_check(session);
if (type == NULL)
return MNL_CB_ERROR;
family = ipset_data_family(data);
switch (session->mode) {
case IPSET_LIST_SAVE:
safe_snprintf(session, "create %s %s",
ipset_data_setname(data),
type->name);
break;
case IPSET_LIST_PLAIN:
safe_snprintf(session, "%sName: %s\n"
"Type: %s\nRevision: %u\nHeader:",
session->printed_set ? "\n" : "",
ipset_data_setname(data),
type->name, type->revision);
break;
case IPSET_LIST_XML:
safe_snprintf(session,
"<ipset name=\"%s\">\n"
"<type>%s</type>\n"
"<revision>%u</revision>\n"
"<header>",
ipset_data_setname(data),
type->name, type->revision);
break;
default:
break;
}
D("type %s, rev %u", type->name, type->revision);
for (i = 0; type->cmd[IPSET_CREATE].args[i] != IPSET_ARG_NONE; i++) {
arg = ipset_keyword(type->cmd[IPSET_CREATE].args[i]);
D("create print arg opt %u (%s) %s", arg->opt,
arg->name[0] ? arg->name[0] : "",
ipset_data_test(data, arg->opt) ? "(yes)" : "(missing)");
if (!arg->print ||
!ipset_data_test(data, arg->opt) ||
(arg->opt == IPSET_OPT_FAMILY &&
family == type->family))
continue;
switch (session->mode) {
case IPSET_LIST_SAVE:
case IPSET_LIST_PLAIN:
if (arg->has_arg == IPSET_NO_ARG) {
safe_snprintf(session, " %s", arg->name[0]);
break;
}
safe_snprintf(session, " %s ", arg->name[0]);
safe_dprintf(session, arg->print, arg->opt);
break;
case IPSET_LIST_XML:
if (arg->has_arg == IPSET_NO_ARG) {
safe_snprintf(session,
"<%s/>", arg->name[0]);
break;
}
safe_snprintf(session, "<%s>", arg->name[0]);
safe_dprintf(session, arg->print, arg->opt);
safe_snprintf(session, "</%s>", arg->name[0]);
break;
default:
break;
}
}
switch (session->mode) {
case IPSET_LIST_SAVE:
safe_snprintf(session, "\n");
break;
case IPSET_LIST_PLAIN:
safe_snprintf(session, "\nSize in memory: ");
safe_dprintf(session, ipset_print_number, IPSET_OPT_MEMSIZE);
safe_snprintf(session, "\nReferences: ");
safe_dprintf(session, ipset_print_number, IPSET_OPT_REFERENCES);
if (ipset_data_test(data, IPSET_OPT_ELEMENTS)) {
safe_snprintf(session, "\nNumber of entries: ");
safe_dprintf(session, ipset_print_number, IPSET_OPT_ELEMENTS);
}
safe_snprintf(session,
session->envopts & IPSET_ENV_LIST_HEADER ?
"\n" : "\nMembers:\n");
break;
case IPSET_LIST_XML:
safe_snprintf(session, "\n<memsize>");
safe_dprintf(session, ipset_print_number, IPSET_OPT_MEMSIZE);
safe_snprintf(session, "</memsize>\n<references>");
safe_dprintf(session, ipset_print_number, IPSET_OPT_REFERENCES);
safe_snprintf(session, "</references>\n");
if (ipset_data_test(data, IPSET_OPT_ELEMENTS)) {
safe_snprintf(session, "<numentries>");
safe_dprintf(session, ipset_print_number, IPSET_OPT_ELEMENTS);
safe_snprintf(session, "</numentries>\n");
}
safe_snprintf(session,
session->envopts & IPSET_ENV_LIST_HEADER ?
"</header>\n" :
"</header>\n<members>\n");
break;
default:
break;
}
session->printed_set++;
session->sort = strncmp(type->name, "hash:", 5) == 0 &&
ipset_envopt_test(session, IPSET_ENV_SORTED);
return MNL_CB_OK;
}
static int
bystrcmp(void *priv, struct list_head *a, struct list_head *b)
{
struct ipset_session *session = priv;
struct ipset_sorted *x = list_entry(a, struct ipset_sorted, list);
struct ipset_sorted *y = list_entry(b, struct ipset_sorted, list);
return strcmp(session->outbuf + x->offset,
session->outbuf + y->offset);
}
static int
print_set_done(struct ipset_session *session, bool callback_done)
{
D("called for %s", session->saved_setname[0] == '\0'
? "NONE" : session->saved_setname);
if (session->sort) {
struct ipset_sorted *pos;
int ret;
/* Print set header */
ret = call_outfn(session);
if (ret)
return MNL_CB_ERROR;
list_sort(session, &session->sorted, bystrcmp);
list_for_each_entry(pos, &session->sorted, list) {
ret = session->print_outfn(session, session->p,
"%s",
session->outbuf + pos->offset);
if (ret < 0)
return MNL_CB_ERROR;
}
list_splice(&session->sorted, &session->pool);
INIT_LIST_HEAD(&session->sorted);
}
switch (session->mode) {
case IPSET_LIST_XML:
if (session->envopts & IPSET_ENV_LIST_SETNAME)
break;
if (session->envopts & IPSET_ENV_LIST_HEADER) {
if (session->saved_setname[0] != '\0')
safe_snprintf(session, "</ipset>\n");
break;
}
if (session->saved_setname[0] != '\0')
safe_snprintf(session, "</members>\n</ipset>\n");
break;
default:
break;
}
if (callback_done && session->mode == IPSET_LIST_XML)
safe_snprintf(session, "</ipsets>\n");
return call_outfn(session) ? MNL_CB_ERROR : MNL_CB_STOP;
}
static int
callback_list(struct ipset_session *session, struct nlattr *nla[],
enum ipset_cmd cmd)
{
struct ipset_data *data = session->data;
if (setjmp(printf_failure)) {
session->saved_setname[0] = '\0';
session->printed_set = 0;
return MNL_CB_ERROR;
}
if (!nla[IPSET_ATTR_SETNAME])
FAILURE("Broken %s kernel message: missing setname!",
cmd2name[cmd]);
ATTR2DATA(session, nla, IPSET_ATTR_SETNAME, cmd_attrs);
D("setname %s", ipset_data_setname(data));
if (session->envopts & IPSET_ENV_LIST_SETNAME &&
session->mode != IPSET_LIST_SAVE) {
if (session->mode == IPSET_LIST_XML)
safe_snprintf(session, "<ipset name=\"%s\"/>\n",
ipset_data_setname(data));
else
safe_snprintf(session, "%s\n",
ipset_data_setname(data));
return call_outfn(session) ? MNL_CB_ERROR : MNL_CB_OK;
}
if (STREQ(ipset_data_setname(data), session->saved_setname)) {
/* Header part already seen */
if (ipset_data_test(data, IPSET_OPT_TYPE) &&
nla[IPSET_ATTR_DATA] != NULL)
FAILURE("Broken %s kernel message: "
"extra DATA received!", cmd2name[cmd]);
} else {
if (nla[IPSET_ATTR_DATA] == NULL)
FAILURE("Broken %s kernel message: "
"missing DATA part!", cmd2name[cmd]);
/* Close previous set printing */
if (session->saved_setname[0] != '\0')
print_set_done(session, false);
}
if (nla[IPSET_ATTR_DATA] != NULL) {
struct nlattr *cattr[IPSET_ATTR_CREATE_MAX+1] = {};
if (!(nla[IPSET_ATTR_TYPENAME] &&
nla[IPSET_ATTR_FAMILY] &&
nla[IPSET_ATTR_REVISION]))
FAILURE("Broken %s kernel message: missing %s!",
cmd2name[cmd],
!nla[IPSET_ATTR_TYPENAME] ? "typename" :
!nla[IPSET_ATTR_FAMILY] ? "family" :
"revision");
/* Reset CREATE specific flags */
ipset_data_flags_unset(data, IPSET_CREATE_FLAGS);
D("nla typename %s",
(char *) mnl_attr_get_payload(nla[IPSET_ATTR_TYPENAME]));
ATTR2DATA(session, nla, IPSET_ATTR_FAMILY, cmd_attrs);
ATTR2DATA(session, nla, IPSET_ATTR_TYPENAME, cmd_attrs);
ATTR2DATA(session, nla, IPSET_ATTR_REVISION, cmd_attrs);
D("head: family %u, typename %s",
ipset_data_family(data),
(const char *) ipset_data_get(data, IPSET_OPT_TYPENAME));
if (mnl_attr_parse_nested(nla[IPSET_ATTR_DATA],
create_attr_cb, cattr) < 0)
FAILURE("Broken %s kernel message: "
"cannot validate DATA attributes!",
cmd2name[cmd]);
if (list_create(session, cattr) != MNL_CB_OK)
return MNL_CB_ERROR;
strcpy(session->saved_setname, ipset_data_setname(data));
}
if (nla[IPSET_ATTR_ADT] != NULL) {
struct nlattr *tb, *adt[IPSET_ATTR_ADT_MAX+1];
mnl_attr_for_each_nested(tb, nla[IPSET_ATTR_ADT]) {
D("ADT attributes for %s", ipset_data_setname(data));
memset(adt, 0, sizeof(adt));
/* Reset ADT specific flags */
ipset_data_flags_unset(data, IPSET_ADT_FLAGS);
if (mnl_attr_parse_nested(tb, adt_attr_cb, adt) < 0)
FAILURE("Broken %s kernel message: "
"cannot validate ADT attributes!",
cmd2name[cmd]);
if (list_adt(session, adt) != MNL_CB_OK)
return MNL_CB_ERROR;
}
if (session->sort)
return MNL_CB_OK;
}
return call_outfn(session) ? MNL_CB_ERROR : MNL_CB_OK;
}
#ifndef IPSET_PROTOCOL_MIN
#define IPSET_PROTOCOL_MIN IPSET_PROTOCOL
#endif
#ifndef IPSET_PROTOCOL_MAX
#define IPSET_PROTOCOL_MAX IPSET_PROTOCOL
#endif
static int
callback_version(struct ipset_session *session, struct nlattr *nla[])
{
uint8_t min, max;
min = max = mnl_attr_get_u8(nla[IPSET_ATTR_PROTOCOL]);
if (nla[IPSET_ATTR_PROTOCOL_MIN]) {
min = mnl_attr_get_u8(nla[IPSET_ATTR_PROTOCOL_MIN]);
D("min: %u", min);
}
if (min > IPSET_PROTOCOL_MAX || max < IPSET_PROTOCOL_MIN)
FAILURE("Cannot communicate with kernel: "
"Kernel support protocol versions %u-%u "
"while userspace supports protocol versions %u-%u",
min, max, IPSET_PROTOCOL_MIN, IPSET_PROTOCOL_MAX);
if (!(session->envopts & IPSET_ENV_QUIET) &&
max != IPSET_PROTOCOL_MAX)
ipset_warn(session,
"Kernel support protocol versions %u-%u "
"while userspace supports protocol versions %u-%u",
min, max, IPSET_PROTOCOL_MIN, IPSET_PROTOCOL_MAX);
session->protocol = MIN(max, IPSET_PROTOCOL_MAX);
session->version_checked = true;
return MNL_CB_STOP;
}
static int
callback_header(struct ipset_session *session, struct nlattr *nla[])
{
const char *setname;
const struct ipset_data *data = session->data;
if (!nla[IPSET_ATTR_SETNAME])
FAILURE("Broken HEADER kernel message: missing setname!");
setname = mnl_attr_get_str(nla[IPSET_ATTR_SETNAME]);
if (!STREQ(setname, ipset_data_setname(data)))
FAILURE("Broken HEADER kernel message: sent setname `%s' "
"does not match with received one `%s'!",
ipset_data_setname(data), setname);
if (!(nla[IPSET_ATTR_TYPENAME] &&
nla[IPSET_ATTR_REVISION] &&
nla[IPSET_ATTR_FAMILY]))
FAILURE("Broken HEADER kernel message: "
"missing attribute '%s'!",
!nla[IPSET_ATTR_TYPENAME] ? "typename" :
!nla[IPSET_ATTR_REVISION] ? "revision" :
"family");
ATTR2DATA(session, nla, IPSET_ATTR_TYPENAME, cmd_attrs);
ATTR2DATA(session, nla, IPSET_ATTR_REVISION, cmd_attrs);
ATTR2DATA(session, nla, IPSET_ATTR_FAMILY, cmd_attrs);
D("got family: %u", ipset_data_family(session->data));
return MNL_CB_STOP;
}
static int
callback_type(struct ipset_session *session, struct nlattr *nla[])
{
const struct ipset_data *data = session->data;
const char *typename, *orig;
if (!(nla[IPSET_ATTR_TYPENAME] &&
nla[IPSET_ATTR_REVISION] &&
nla[IPSET_ATTR_FAMILY]))
FAILURE("Broken TYPE kernel message: "
"missing attribute '%s'!",
!nla[IPSET_ATTR_TYPENAME] ? "typename" :
!nla[IPSET_ATTR_REVISION] ? "revision" :
"family");
typename = mnl_attr_get_str(nla[IPSET_ATTR_TYPENAME]);
orig = ipset_data_get(data, IPSET_OPT_TYPENAME);
if (!STREQ(typename, orig))
FAILURE("Broken TYPE kernel message: sent typename `%s' "
"does not match with received one `%s'!",
orig, typename);
ATTR2DATA(session, nla, IPSET_ATTR_TYPENAME, cmd_attrs);
ATTR2DATA(session, nla, IPSET_ATTR_REVISION, cmd_attrs);
ATTR2DATA(session, nla, IPSET_ATTR_FAMILY, cmd_attrs);
if (nla[IPSET_ATTR_REVISION_MIN])
ATTR2DATA(session, nla, IPSET_ATTR_REVISION_MIN, cmd_attrs);
return MNL_CB_STOP;
}
static int
cmd_attr_cb(const struct nlattr *attr, void *data)
{
return generic_data_attr_cb(attr, data, IPSET_ATTR_CMD_MAX, cmd_attrs);
}
#if 0
static int
mnl_attr_parse_dbg(const struct nlmsghdr *nlh, int offset,
mnl_attr_cb_t cb, void *data)
{
int ret = MNL_CB_OK;
struct nlattr *attr = mnl_nlmsg_get_payload_offset(nlh, offset);
int len = nlh->nlmsg_len - MNL_NLMSG_HDRLEN - MNL_ALIGN(offset);
while (mnl_attr_ok(attr, len)) {
D("attr: type %u, attrlen %u, len %u",
mnl_attr_get_type(attr), attr->nla_len, len);
if (cb && (ret = cb(attr, data)) <= MNL_CB_STOP)
return ret;
attr = mnl_attr_next(attr, &len);
}
return ret;
}
#endif
static int
callback_data(const struct nlmsghdr *nlh, void *data)
{
struct ipset_session *session = data;
struct nlattr *nla[IPSET_ATTR_CMD_MAX+1] = {};
uint8_t proto, cmd;
int ret = MNL_CB_OK, nfmsglen = MNL_ALIGN(sizeof(struct nfgenmsg));
D("called, nlmsg_len %u", nlh->nlmsg_len);
cmd = ipset_get_nlmsg_type(nlh);
if (cmd == IPSET_CMD_LIST && session->cmd == IPSET_CMD_SAVE)
/* Kernel always send IPSET_CMD_LIST */
cmd = IPSET_CMD_SAVE;
if (cmd != session->cmd)
FAILURE("Protocol error, we sent command %s "
"and received %s[%u]",
cmd2name[session->cmd],
cmd < IPSET_MSG_MAX ? cmd2name[cmd] : "unknown", cmd);
if (mnl_attr_parse(nlh, nfmsglen, cmd_attr_cb, nla) < MNL_CB_STOP)
FAILURE("Broken %s kernel message: "
"cannot validate and parse attributes",
cmd2name[cmd]);
if (!nla[IPSET_ATTR_PROTOCOL])
FAILURE("Sad, sad day: kernel message %s "
"does not carry the protocol version.",
cmd2name[cmd]);
proto = mnl_attr_get_u8(nla[IPSET_ATTR_PROTOCOL]);
/* Check protocol */
if (cmd != IPSET_CMD_PROTOCOL && proto != session->protocol)
FAILURE("Giving up: kernel protocol version %u "
"does not match our protocol version %u",
proto, session->protocol);
D("Message: %s", cmd2name[cmd]);
switch (cmd) {
case IPSET_CMD_LIST:
case IPSET_CMD_SAVE:
ret = callback_list(session, nla, cmd);
D("flag multi: %u", nlh->nlmsg_flags & NLM_F_MULTI);
if (ret >= MNL_CB_STOP && !(nlh->nlmsg_flags & NLM_F_MULTI))
ret = print_set_done(session, false);
break;
case IPSET_CMD_PROTOCOL:
if (!session->version_checked)
ret = callback_version(session, nla);
break;
case IPSET_CMD_HEADER:
ret = callback_header(session, nla);
break;
case IPSET_CMD_TYPE:
ret = callback_type(session, nla);
break;
default:
FAILURE("Data message received when not expected at %s",
cmd2name[session->cmd]);
}
D("return code: %s", ret == MNL_CB_STOP ? "stop" :
ret == MNL_CB_OK ? "ok" : "error");
return ret;
}
static int
callback_done(const struct nlmsghdr *nlh UNUSED, void *data)
{
struct ipset_session *session = data;
D(" called");
if (session->cmd == IPSET_CMD_LIST || session->cmd == IPSET_CMD_SAVE)
return print_set_done(session, true);
FAILURE("Invalid message received in non LIST or SAVE state.");
}
static int
decode_errmsg(struct ipset_session *session, const struct nlmsghdr *nlh)
{
const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
const struct nlmsghdr *msg = &err->msg;
struct nlattr *nla[IPSET_ATTR_CMD_MAX+1] = {};
enum ipset_cmd cmd;
int nfmsglen = MNL_ALIGN(sizeof(struct nfgenmsg));
if (nlh->nlmsg_len < (uint32_t) MNL_ALIGN(sizeof(struct nlmsgerr)) ||
nlh->nlmsg_len < MNL_ALIGN(sizeof(struct nlmsgerr))
+ msg->nlmsg_len)
FAILURE("Broken error report message received.");
cmd = ipset_get_nlmsg_type(msg);
D("nlsmg_len: %u", msg->nlmsg_len);
if (cmd != session->cmd)
FAILURE("Protocol error, we sent command %s "
"and received error report for %s[%u]",
cmd2name[session->cmd],
cmd < IPSET_MSG_MAX ? cmd2name[cmd] : "unknown", cmd);
if (mnl_attr_parse(msg, nfmsglen, cmd_attr_cb, nla) < MNL_CB_STOP)
FAILURE("Broken %s error report message: "
"cannot validate attributes",
cmd2name[cmd]);
if (!nla[IPSET_ATTR_PROTOCOL])
FAILURE("Broken %s error report message: "
"missing protocol attribute",
cmd2name[cmd]);
if (nla[IPSET_ATTR_LINENO]) {
session->lineno = mnl_attr_get_u32(nla[IPSET_ATTR_LINENO]);
if (nla[IPSET_ATTR_LINENO]->nla_type & NLA_F_NET_BYTEORDER)
session->lineno = ntohl(session->lineno);
}
return ipset_errcode(session, cmd, -err->error);
}
static int
callback_error(const struct nlmsghdr *nlh, void *cbdata)
{
struct ipset_session *session = cbdata;
struct ipset_data *data = session->data;
const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
int ret = MNL_CB_ERROR;
D(" called, cmd %s", cmd2name[session->cmd]);
if (nlh->nlmsg_len < mnl_nlmsg_size(sizeof(struct nlmsgerr)))
FAILURE("Broken error message received.");
if (err->error == 0) {
/* ACK */
ret = MNL_CB_STOP;
switch (session->cmd) {
case IPSET_CMD_CREATE:
/* Add successfully created set to the cache */
ipset_cache_add(ipset_data_setname(data),
ipset_data_get(data, IPSET_OPT_TYPE),
ipset_data_family(data));
break;
case IPSET_CMD_DESTROY:
/* Delete destroyed sets from the cache */
ipset_cache_del(ipset_data_setname(data));
/* Fall through */
case IPSET_CMD_FLUSH:
break;
case IPSET_CMD_RENAME:
ipset_cache_rename(ipset_data_setname(data),
ipset_data_get(data,
IPSET_OPT_SETNAME2));
break;
case IPSET_CMD_SWAP:
ipset_cache_swap(ipset_data_setname(data),
ipset_data_get(data,
IPSET_OPT_SETNAME2));
break;
case IPSET_CMD_TEST:
if (!(session->envopts & IPSET_ENV_QUIET)) {
ipset_print_elem(session->report,
IPSET_ERRORBUFLEN,
session->data,
IPSET_OPT_NONE, 0);
ipset_warn(session, " is in set %s.",
ipset_data_setname(data));
}
/* Fall through */
case IPSET_CMD_ADD:
case IPSET_CMD_DEL:
break;
case IPSET_CMD_LIST:
case IPSET_CMD_SAVE:
/* No set in kernel */
print_set_done(session, true);
break;
default:
FAILURE("ACK message received to command %s[%u], "
"which is not expected",
session->cmd < IPSET_MSG_MAX
? cmd2name[session->cmd] : "unknown",
session->cmd);
}
return ret;
}
D("nlmsgerr error: %u", -err->error);
/* Error messages */
/* Special case for IPSET_CMD_TEST */
if (session->cmd == IPSET_CMD_TEST &&
err->error == -IPSET_ERR_EXIST) {
if (!(session->envopts & IPSET_ENV_QUIET)) {
ipset_print_elem(session->report, IPSET_ERRORBUFLEN,
session->data, IPSET_OPT_NONE, 0);
ipset_notice(session, " is NOT in set %s.",
ipset_data_setname(data));
}
return ret;
}
decode_errmsg(session, nlh);
return ret;
}
static int
callback_noop(const struct nlmsghdr *nlh UNUSED, void *data UNUSED)
{
return MNL_CB_OK;
}
/*
* Build and send messages
*/
static inline int
open_nested(struct ipset_session *session, struct nlmsghdr *nlh, int attr)
{
if (nlh->nlmsg_len + MNL_ATTR_HDRLEN > session->bufsize)
return 1;
session->nested[session->nestid++] = mnl_attr_nest_start(nlh, attr);
return 0;
}
static inline void
close_nested(struct ipset_session *session, struct nlmsghdr *nlh)
{
mnl_attr_nest_end(nlh, session->nested[session->nestid-1]);
session->nested[--session->nestid] = NULL;
}
static size_t
attr_len(const struct ipset_attr_policy *attr, uint8_t family, uint16_t *flags)
{
switch (attr->type) {
case MNL_TYPE_NESTED:
if (attr->len)
return attr->len;
*flags = NLA_F_NET_BYTEORDER;
return family == NFPROTO_IPV4 ? sizeof(uint32_t)
: sizeof(struct in6_addr);
case MNL_TYPE_U64:
*flags = NLA_F_NET_BYTEORDER;
return sizeof(uint64_t);
case MNL_TYPE_U32:
*flags = NLA_F_NET_BYTEORDER;
return sizeof(uint32_t);
case MNL_TYPE_U16:
*flags = NLA_F_NET_BYTEORDER;
return sizeof(uint16_t);
case MNL_TYPE_U8:
return sizeof(uint8_t);
default:
return attr->len;
}
}
#define BUFFER_FULL(bufsize, nlmsg_len, nestlen, attrlen) \
(nlmsg_len + nestlen + MNL_ATTR_HDRLEN + MNL_ALIGN(alen) + \
MNL_ALIGN(sizeof(struct nlmsgerr)) > bufsize)
static int
rawdata2attr(struct ipset_session *session, struct nlmsghdr *nlh,
const void *d, int type, uint8_t family,
const struct ipset_attr_policy attrs[])
{
const struct ipset_attr_policy *attr;
int alen;
uint16_t flags = 0;
attr = &attrs[type];
if (attr->type == MNL_TYPE_NESTED) {
/* IP addresses */
struct nlattr *nested;
int atype = family == NFPROTO_IPV4 ? IPSET_ATTR_IPADDR_IPV4
: IPSET_ATTR_IPADDR_IPV6;
alen = attr_len(attr, family, &flags);
if (BUFFER_FULL(session->bufsize, nlh->nlmsg_len,
MNL_ATTR_HDRLEN, alen))
return 1;
nested = mnl_attr_nest_start(nlh, type);
D("family: %s", family == NFPROTO_IPV4 ? "INET" :
family == NFPROTO_IPV6 ? "INET6" : "UNSPEC");
mnl_attr_put(nlh, atype | flags, alen, d);
mnl_attr_nest_end(nlh, nested);
return 0;
}
alen = attr_len(attr, family, &flags);
if (BUFFER_FULL(session->bufsize, nlh->nlmsg_len, 0, alen))
return 1;
switch (attr->type) {
case MNL_TYPE_U64: {
uint64_t value = htobe64(*(const uint64_t *)d);
mnl_attr_put(nlh, type | flags, alen, &value);
return 0;
}
case MNL_TYPE_U32: {
uint32_t value = htonl(*(const uint32_t *)d);
mnl_attr_put(nlh, type | flags, alen, &value);
return 0;
}
case MNL_TYPE_U16: {
uint16_t value = htons(*(const uint16_t *)d);
mnl_attr_put(nlh, type | flags, alen, &value);
return 0;
}
case MNL_TYPE_NUL_STRING:
alen = strlen((const char *)d) + 1;
break;
default:
break;
}
mnl_attr_put(nlh, type | flags, alen, d);
return 0;
}
static int
data2attr(struct ipset_session *session, struct nlmsghdr *nlh,
struct ipset_data *data, int type, uint8_t family,
const struct ipset_attr_policy attrs[])
{
const struct ipset_attr_policy *attr = &attrs[type];
return rawdata2attr(session, nlh, ipset_data_get(data, attr->opt),
type, family, attrs);
}
#define ADDATTR_PROTOCOL(nlh, protocol) \
mnl_attr_put_u8(nlh, IPSET_ATTR_PROTOCOL, protocol)
#define ADDATTR(session, nlh, data, type, family, attrs) \
data2attr(session, nlh, data, type, family, attrs)
#define ADDATTR_SETNAME(session, nlh, data) \
data2attr(session, nlh, data, IPSET_ATTR_SETNAME, NFPROTO_IPV4, \
cmd_attrs)
#define ADDATTR_IF(session, nlh, data, type, family, attrs) \
ipset_data_test(data, attrs[type].opt) ? \
data2attr(session, nlh, data, type, family, attrs) : 0
#define ADDATTR_RAW(session, nlh, data, type, attrs) \
rawdata2attr(session, nlh, data, type, NFPROTO_IPV4, attrs)
static void
addattr_create(struct ipset_session *session,
struct nlmsghdr *nlh, struct ipset_data *data, uint8_t family)
{
int i;
for (i = IPSET_ATTR_UNSPEC + 1; i <= IPSET_ATTR_CREATE_MAX; i++)
ADDATTR_IF(session, nlh, data, i, family, create_attrs);
}
static int
addattr_adt(struct ipset_session *session,
struct nlmsghdr *nlh, struct ipset_data *data, uint8_t family)
{
int i;
for (i = IPSET_ATTR_UNSPEC + 1; i <= IPSET_ATTR_ADT_MAX; i++)
if (ADDATTR_IF(session, nlh, data, i, family, adt_attrs))
return 1;
return 0;
}
#define PRIVATE_MSG_BUFLEN 256
static int
build_send_private_msg(struct ipset_session *session, enum ipset_cmd cmd)
{
char buffer[PRIVATE_MSG_BUFLEN] __attribute__ ((aligned)) = {};
struct nlmsghdr *nlh = (void *)buffer;
struct ipset_data *data = session->data;
int len = PRIVATE_MSG_BUFLEN, ret;
enum ipset_cmd saved = session->cmd;
/* Initialize header */
session->transport->fill_hdr(session->handle, cmd, buffer, len, 0);
ADDATTR_PROTOCOL(nlh,
cmd == IPSET_CMD_PROTOCOL ? IPSET_PROTOCOL : session->protocol);
switch (cmd) {
case IPSET_CMD_PROTOCOL:
break;
case IPSET_CMD_HEADER:
if (!ipset_data_test(data, IPSET_SETNAME))
return ipset_err(session,
"Invalid internal HEADER command: "
"missing setname");
ADDATTR_SETNAME(session, nlh, data);
break;
case IPSET_CMD_TYPE:
if (!ipset_data_test(data, IPSET_OPT_TYPENAME))
return ipset_err(session,
"Invalid internal TYPE command: "
"missing settype");
ADDATTR(session, nlh, data, IPSET_ATTR_TYPENAME,
NFPROTO_IPV4, cmd_attrs);
if (ipset_data_test(data, IPSET_OPT_FAMILY))
ADDATTR(session, nlh, data, IPSET_ATTR_FAMILY,
NFPROTO_IPV4, cmd_attrs);
else
/* bitmap:port and list:set types */
mnl_attr_put_u8(nlh, IPSET_ATTR_FAMILY, NFPROTO_UNSPEC);
break;
default:
return ipset_err(session, "Internal error: "
"unknown private command %u", cmd);
}
/* Backup, then restore real command */
session->cmd = cmd;
ret = session->transport->query(session->handle, buffer, len);
session->cmd = saved;
return ret;
}
static inline bool
may_aggregate_ad(struct ipset_session *session, enum ipset_cmd cmd)
{
return session->lineno != 0 &&
(cmd == IPSET_CMD_ADD || cmd == IPSET_CMD_DEL) &&
cmd == session->cmd &&
STREQ(ipset_data_setname(session->data), session->saved_setname);
}
static int
build_msg(struct ipset_session *session, bool aggregate)
{
struct nlmsghdr *nlh = session->buffer;
struct ipset_data *data = session->data;
/* Public commands */
D("cmd %s, nlmsg_len: %u", cmd2name[session->cmd], nlh->nlmsg_len);
if (nlh->nlmsg_len == 0) {
/* Initialize header */
aggregate = false;
session->transport->fill_hdr(session->handle,
session->cmd,
session->buffer,
session->bufsize,
session->envopts);
ADDATTR_PROTOCOL(nlh, session->protocol);
}
D("Protocol added, aggregate %s", aggregate ? "yes" : "no");
switch (session->cmd) {
case IPSET_CMD_CREATE: {
const struct ipset_type *type;
/* Sanity checkings */
if (!ipset_data_test(data, IPSET_SETNAME))
return ipset_err(session,
"Invalid create command: missing setname");
if (!ipset_data_test(data, IPSET_OPT_TYPE))
return ipset_err(session,
"Invalid create command: missing settype");
type = ipset_data_get(data, IPSET_OPT_TYPE);
/* Core attributes:
* setname, typename, revision, family, flags (optional) */
ADDATTR_SETNAME(session, nlh, data);
ADDATTR(session, nlh, data, IPSET_ATTR_TYPENAME,
NFPROTO_IPV4, cmd_attrs);
ADDATTR_RAW(session, nlh, &type->revision,
IPSET_ATTR_REVISION, cmd_attrs);
D("family: %u, type family %u",
ipset_data_family(data), type->family);
if (ipset_data_test(data, IPSET_OPT_FAMILY))
ADDATTR(session, nlh, data, IPSET_ATTR_FAMILY,
NFPROTO_IPV4, cmd_attrs);
else
/* bitmap:port and list:set types */
mnl_attr_put_u8(nlh, IPSET_ATTR_FAMILY, NFPROTO_UNSPEC);
/* Type-specific create attributes */
D("call open_nested");
open_nested(session, nlh, IPSET_ATTR_DATA);
addattr_create(session, nlh, data, type->family);
D("call close_nested");
close_nested(session, nlh);
break;
}
case IPSET_CMD_DESTROY:
case IPSET_CMD_FLUSH:
case IPSET_CMD_SAVE:
if (ipset_data_test(data, IPSET_SETNAME))
ADDATTR_SETNAME(session, nlh, data);
break;
case IPSET_CMD_LIST: {
uint32_t flags = 0;
if (session->envopts & IPSET_ENV_LIST_SETNAME)
flags |= IPSET_FLAG_LIST_SETNAME;
if (session->envopts & IPSET_ENV_LIST_HEADER)
flags |= IPSET_FLAG_LIST_HEADER;
if (ipset_data_test(data, IPSET_SETNAME))
ADDATTR_SETNAME(session, nlh, data);
if (flags && session->mode != IPSET_LIST_SAVE) {
ipset_data_set(data, IPSET_OPT_FLAGS, &flags);
ADDATTR(session, nlh, data, IPSET_ATTR_FLAGS,
NFPROTO_IPV4, cmd_attrs);
}
break;
}
case IPSET_CMD_RENAME:
case IPSET_CMD_SWAP:
if (!ipset_data_test(data, IPSET_SETNAME))
return ipset_err(session,
"Invalid %s command: missing from-setname",
session->cmd == IPSET_CMD_SWAP ? "swap" :
"rename");
if (!ipset_data_test(data, IPSET_OPT_SETNAME2))
return ipset_err(session,
"Invalid %s command: missing to-setname",
session->cmd == IPSET_CMD_SWAP ? "swap" :
"rename");
ADDATTR_SETNAME(session, nlh, data);
ADDATTR_RAW(session, nlh,
ipset_data_get(data, IPSET_OPT_SETNAME2),
IPSET_ATTR_SETNAME2, cmd_attrs);
break;
case IPSET_CMD_ADD:
case IPSET_CMD_DEL: {
const struct ipset_type *type;
if (!aggregate) {
/* Setname, type not checked/added yet */
if (!ipset_data_test(data, IPSET_SETNAME))
return ipset_err(session,
"Invalid %s command: missing setname",
session->cmd == IPSET_CMD_ADD ? "add" :
"del");
if (!ipset_data_test(data, IPSET_OPT_TYPE))
return ipset_err(session,
"Invalid %s command: missing settype",
session->cmd == IPSET_CMD_ADD ? "add" :
"del");
/* Core options: setname */
ADDATTR_SETNAME(session, nlh, data);
if (session->lineno != 0) {
/* Restore mode */
ADDATTR_RAW(session, nlh, &session->lineno,
IPSET_ATTR_LINENO, cmd_attrs);
open_nested(session, nlh, IPSET_ATTR_ADT);
}
}
type = ipset_data_get(data, IPSET_OPT_TYPE);
D("family: %u, type family %u",
ipset_data_family(data), type->family);
if (open_nested(session, nlh, IPSET_ATTR_DATA)) {
D("open_nested failed");
return 1;
}
if (addattr_adt(session, nlh, data, ipset_data_family(data)) ||
ADDATTR_RAW(session, nlh, &session->lineno,
IPSET_ATTR_LINENO, cmd_attrs)) {
/* Cancel last, unfinished nested attribute */
mnl_attr_nest_cancel(nlh,
session->nested[session->nestid-1]);
session->nested[--session->nestid] = NULL;
return 1;
}
close_nested(session, nlh);
break;
}
case IPSET_CMD_TEST: {
const struct ipset_type *type;
/* Return codes are not aggregated, so tests cannot be either */
/* Setname, type not checked/added yet */
if (!ipset_data_test(data, IPSET_SETNAME))
return ipset_err(session,
"Invalid test command: missing setname");
if (!ipset_data_test(data, IPSET_OPT_TYPE))
return ipset_err(session,
"Invalid test command: missing settype");
type = ipset_data_get(data, IPSET_OPT_TYPE);
D("family: %u, type family %u",
ipset_data_family(data), type->family);
ADDATTR_SETNAME(session, nlh, data);
open_nested(session, nlh, IPSET_ATTR_DATA);
addattr_adt(session, nlh, data, ipset_data_family(data));
close_nested(session, nlh);
break;
}
default:
return ipset_err(session, "Internal error: unknown command %u",
session->cmd);
}
return 0;
}
/**
* ipset_commit - commit buffered commands
* @session: session structure
*
* Commit buffered commands, if there are any.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_commit(struct ipset_session *session)
{
struct nlmsghdr *nlh;
int ret = 0, i;
assert(session);
nlh = session->buffer;
D("send buffer: len %u, cmd %s",
nlh->nlmsg_len, cmd2name[session->cmd]);
if (nlh->nlmsg_len == 0)
/* Nothing to do */
return 0;
/* Close nested data blocks */
for (i = session->nestid - 1; i >= 0; i--)
close_nested(session, nlh);
/* Send buffer */
ret = session->transport->query(session->handle,
session->buffer,
session->bufsize);
/* Reset saved data and nested state */
session->saved_setname[0] = '\0';
session->printed_set = 0;
for (i = session->nestid - 1; i >= 0; i--)
session->nested[i] = NULL;
session->nestid = 0;
nlh->nlmsg_len = 0;
D("ret: %d", ret);
if (ret < 0) {
if (session->report[0] != '\0')
return -1;
else
return ipset_err(session,
"Internal protocol error");
}
return 0;
}
static mnl_cb_t cb_ctl[] = {
[NLMSG_NOOP] = callback_noop,
[NLMSG_ERROR] = callback_error,
[NLMSG_DONE] = callback_done,
[NLMSG_OVERRUN] = callback_noop,
[NLMSG_MIN_TYPE] = callback_data,
};
static inline struct ipset_handle *
init_transport(struct ipset_session *session)
{
session->handle = session->transport->init(cb_ctl, session);
return session->handle;
}
/**
* ipset_cmd - execute a command
* @session: session structure
* @cmd: command to execute
* @lineno: command line number in restore mode
*
* Execute - or prepare/buffer in restore mode - a command.
* It is the caller responsibility that the data field be filled out
* with all required parameters for a successful execution.
* The data field is cleared after this function call for the public
* commands.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_cmd(struct ipset_session *session, enum ipset_cmd cmd, uint32_t lineno)
{
struct ipset_data *data;
bool aggregate = false;
int ret = -1;
assert(session);
if (cmd < IPSET_CMD_NONE || cmd >= IPSET_MSG_MAX)
return 0;
/* Initialize transport method if not done yet */
if (session->handle == NULL && init_transport(session) == NULL)
return ipset_err(session,
"Cannot open session to kernel.");
data = session->data;
/* Check protocol version once */
if (!session->version_checked) {
if (build_send_private_msg(session, IPSET_CMD_PROTOCOL) < 0)
return -1;
if (ipset_session_report_type(session) == IPSET_WARNING &&
cmd != IPSET_CMD_NONE)
/* Suppress protocol warning */
ipset_session_report_reset(session);
}
/* IPSET_CMD_NONE: check protocol version only */
if (cmd == IPSET_CMD_NONE)
return 0;
/* Private commands */
if (cmd == IPSET_CMD_TYPE || cmd == IPSET_CMD_HEADER)
return build_send_private_msg(session, cmd);
/* Check aggregatable commands */
aggregate = may_aggregate_ad(session, cmd);
if (!aggregate) {
/* Flush possible aggregated commands */
ret = ipset_commit(session);
if (ret < 0)
return ret;
}
/* Real command: update lineno too */
session->cmd = cmd;
session->lineno = lineno;
/* Set default output mode */
if (cmd == IPSET_CMD_LIST) {
if (session->mode == IPSET_LIST_NONE)
session->mode = IPSET_LIST_PLAIN;
} else if (cmd == IPSET_CMD_SAVE) {
if (session->mode == IPSET_LIST_NONE)
session->mode = IPSET_LIST_SAVE;
}
/* Start the root element in XML mode */
if ((cmd == IPSET_CMD_LIST || cmd == IPSET_CMD_SAVE) &&
session->mode == IPSET_LIST_XML)
safe_snprintf(session, "<ipsets>\n");
D("next: build_msg");
/* Build new message or append buffered commands */
ret = build_msg(session, aggregate);
D("build_msg returned %u", ret);
if (ret > 0) {
/* Buffer is full, send buffered commands */
ret = ipset_commit(session);
if (ret < 0)
goto cleanup;
ret = build_msg(session, false);
D("build_msg 2 returned %u", ret);
}
if (ret < 0)
goto cleanup;
D("past: build_msg");
/* We have to save the type for error handling */
session->saved_type = ipset_data_get(data, IPSET_OPT_TYPE);
if (session->lineno != 0 &&
(cmd == IPSET_CMD_ADD || cmd == IPSET_CMD_DEL)) {
/* Save setname for the next possible aggregated restore line */
strcpy(session->saved_setname, ipset_data_setname(data));
ipset_data_reset(data);
/* Don't commit: we may aggregate next command */
ret = 0;
goto cleanup;
}
D("call commit");
ret = ipset_commit(session);
cleanup:
D("reset data");
ipset_data_reset(data);
return ret;
}
static
int __attribute__ ((format (printf, 3, 4)))
default_print_outfn(struct ipset_session *session, void *p UNUSED,
const char *fmt, ...)
{
int len;
va_list args;
va_start(args, fmt);
len = vfprintf(session->ostream, fmt, args);
va_end(args);
return len;
}
/**
* ipset_session_print_outfn - set session output printing function
* @session: session structure
* @outfn: output printing function
* @p: pointer to private area
*
* Set the session output printing function. If the @outfn is NULL,
* then the default output function is configured. You can set
* the @p pointer to a private area: the output printing function
* is called with @p in one of its arguments.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_session_print_outfn(struct ipset_session *session,
ipset_print_outfn outfn,
void *p)
{
session->print_outfn = outfn ? outfn : default_print_outfn;
session->p = p;
return 0;
}
/**
* ipset_session_init - initialize an ipset session
* @outfn: output printing function
* @p: pointer to private area
*
* Initialize an ipset session by allocating a session structure
* and filling out with the initialization data. The function
* calls ipset_session_print_outfn() to set @print_outfn, @p.
*
* Returns the created session sctructure on success or NULL.
*/
struct ipset_session *
ipset_session_init(ipset_print_outfn print_outfn, void *p)
{
struct ipset_session *session;
size_t bufsize = getpagesize();
/* Create session object */
session = calloc(1, sizeof(struct ipset_session) + bufsize);
if (session == NULL)
return NULL;
session->outbuf = calloc(1, IPSET_OUTBUFLEN);
if (session->outbuf == NULL)
goto free_session;
session->outbuflen = IPSET_OUTBUFLEN;
session->bufsize = bufsize;
session->buffer = session + 1;
session->istream = stdin;
session->ostream = stdout;
session->protocol = IPSET_PROTOCOL;
INIT_LIST_HEAD(&session->sorted);
INIT_LIST_HEAD(&session->pool);
/* The single transport method yet */
session->transport = &ipset_mnl_transport;
/* Output function */
ipset_session_print_outfn(session, print_outfn, p);
/* Initialize data structures */
session->data = ipset_data_init();
if (session->data == NULL)
goto free_outbuf;
ipset_cache_init();
return session;
free_outbuf:
free(session->outbuf);
free_session:
free(session);
return NULL;
}
/**
* ipset_session_io_full - set full IO for the session
* @session: session structure
* @filename: filename
* @what: operate on input/output
*
* The normal "-file" CLI interface does not provide an interface
* to set both the input (restore) and output (list/save) for
* a session. This function makes it possible to configure those.
*
* When a filename for input is passed, then the file will be opened
* for reading.
* When a filename for output is passed, then the file will be opened
* for writing.
* Previously opened files are closed.
* If NULL is passed as filename, stdin/stdout is set.
* Input/output files can be set separatedly.
* The function returns error if the file cannot be opened or
* normal IO mode is already set.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_session_io_full(struct ipset_session *session, const char *filename,
enum ipset_io_type what)
{
FILE *f;
assert(session);
if (session->normal_io)
return ipset_err(session,
"Normal IO is in use, full IO cannot be selected");
switch (what) {
case IPSET_IO_INPUT:
if (session->istream != stdin)
fclose(session->istream);
if (!filename) {
session->istream = stdin;
} else {
f = fopen(filename, "r");
if (!f)
return ipset_err(session,
"Cannot open %s for reading: %s",
filename, strerror(errno));
session->istream = f;
}
break;
case IPSET_IO_OUTPUT:
if (session->ostream != stdout)
fclose(session->ostream);
if (!filename) {
session->ostream = stdout;
} else {
f = fopen(filename, "w");
if (!f)
return ipset_err(session,
"Cannot open %s for writing: %s",
filename, strerror(errno));
session->ostream = f;
}
break;
default:
return ipset_err(session,
"Library error, invalid ipset_io_type");
}
session->full_io = !(session->istream == stdin &&
session->ostream == stdout);
return 0;
}
/**
* ipset_session_io_normal - set normal IO for the session
* @session: session structure
* @filename: filename
* @what: operate on input/output
*
* The normal "-file" CLI interface to set either the input (restore)
* or output (list/save) for a session. This function does not make
* possible to set both independently.
*
* When a filename for input is passed, then the file will be opened
* for reading.
* When a filename for output is passed, then the file will be opened
* for writing.
* Previously opened files are closed.
* If NULL is passed as filename, stdin/stdout is set.
* Input/output files cannot be set separatedly.
* The function returns error if the file cannot be opened or
* full IO mode is already set.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_session_io_normal(struct ipset_session *session, const char *filename,
enum ipset_io_type what)
{
FILE *f;
assert(session);
assert(filename);
if (session->full_io)
return ipset_err(session,
"Full IO is in use, normal IO cannot be selected");
if (session->istream != stdin) {
fclose(session->istream);
session->istream = stdin;
}
if (session->ostream != stdout) {
fclose(session->ostream);
session->ostream = stdout;
}
switch (what) {
case IPSET_IO_INPUT:
f = fopen(filename, "r");
if (!f)
return ipset_err(session,
"Cannot open %s for reading: %s",
filename, strerror(errno));
session->istream = f;
break;
case IPSET_IO_OUTPUT:
f = fopen(filename, "w");
if (!f)
return ipset_err(session,
"Cannot open %s for writing: %s",
filename, strerror(errno));
session->ostream = f;
break;
default:
return ipset_err(session,
"Library error, invalid ipset_io_type");
}
session->normal_io = !(session->istream == stdin &&
session->ostream == stdout);
return 0;
}
/**
* ipset_session_io_stream - returns the input or output stream
* @what: operate on input/output
*
* Returns the input or output stream of the session.
*/
FILE *
ipset_session_io_stream(struct ipset_session *session,
enum ipset_io_type what)
{
switch (what) {
case IPSET_IO_INPUT:
return session->istream;
case IPSET_IO_OUTPUT:
return session->ostream;
default:
return NULL;
}
}
/**
* ipset_session_io_close - closes the input or output stream
* @what: operate on input/output
*
* Closes the input or output stream of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_session_io_close(struct ipset_session *session,
enum ipset_io_type what)
{
switch (what) {
case IPSET_IO_INPUT:
if (session->istream != stdin) {
fclose(session->istream);
session->istream = stdin;
}
break;
case IPSET_IO_OUTPUT:
if (session->ostream != stdout) {
fclose(session->ostream);
session->ostream = stdout;
}
break;
default:
break;
}
return 0;
}
/**
* ipset_session_fini - destroy an ipset session
* @session: session structure
*
* Destroy an ipset session: release the created structures.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_session_fini(struct ipset_session *session)
{
struct ipset_sorted *pos, *n;
assert(session);
if (session->handle)
session->transport->fini(session->handle);
if (session->data)
ipset_data_fini(session->data);
if (session->istream != stdin)
fclose(session->istream);
if (session->ostream != stdout)
fclose(session->ostream);
ipset_cache_fini();
list_for_each_entry_safe(pos, n, &session->sorted, list) {
list_del(&pos->list);
free(pos);
}
list_for_each_entry_safe(pos, n, &session->pool, list) {
list_del(&pos->list);
free(pos);
}
free(session->outbuf);
free(session);
return 0;
}
#ifdef IPSET_DEBUG
#include "debug.c"
#endif
|